在 HTML5 画布上动画绘制路径

2023-06-21前端开发问题
31

本文介绍了在 HTML5 画布上动画绘制路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的问题是如何为两点之间的路径绘制动画.

My problem is how to animate the drawing of a path between two points.

考虑两点之间的曲线或路径,A &B. 我可以使用 Konvajs 中的线条绘制功能在画布上轻松绘制.

Consider a curved line or path between two points, A & B. I can draw this easily on the canvas using the line drawing functions in Konvajs.

但是,我真正想要的是对线的显示进行动画处理,使其从 A 点开始并逐渐绘制到 B 点.显示应该是动画的,这样我就可以应用令人愉悦的缓动.

However, what I actually want is to animate the revealing of the line so that it starts from point A and progressively draws to point B. The reveal should be animated so I can apply pleasing easings.

作为一个比较示例,请参阅本网站 https://coggle.it/ 上的简短视频,其中视频展示了新盒子的创建以及将其与旧盒子连接起来的线条.

As a comparable example, see the brief video on this site https://coggle.it/ where the video shows the creation of a new box and the line draws to connect it to the old.

推荐答案

这是一个潜在的答案(特别感谢 @markov00 在 SVG 中使用相同的技术).它通过操作路径 dashOffset 和 dash 属性来工作.在 Jake Archibald 的帖子中对技术进行了很好的解释其中还包括一个我发现非常有用的滑块互动实验.

Here is a potential answer (special thanks to @markov00 re same technique in SVG). It works by manipulating the path dashOffset and dash attributes. There is an excellent explanation of the technique here in a post by Jake Archibald which also includes an interactive experiment with sliders which I found very useful.

我已尝试使演示尽可能轻量级并仅展示技术 - 尽管我添加了一个滑块和一些 UI 以帮助理解该过程.jquery 的使用仅用于该技术不需要的 UI 部分.

I have tried to make the demo as lightweight as possible and just show the technique - though I added a slider and some UI to help understand the process. The use of jquery is only for the UI parts which are not needed for the technique.

几点:

  • 此处的演示使用 3 条直线段组成的路径.但我尝试了曲线和组合路径,并且该技术在这些情况下也适用 - 所以任何路径都应该有效.
  • 我发现在路径上使用近距离路径命令 (z) 会导致路径长度函数在真实距离上变短.这显示为在任一端保持描边或间隙的路径量,其大小取决于 first 和最后关闭路径.
  • 路径长度几乎总是十进制,所以不要尝试将所有内容都作为整数,因为最终您会发现您的笔划稍微过长或过短.

要将其用于动画和缓动等,请从滑块更改事件中获取几行并将它们粘贴到帧回调中,根据您的情况操作数学.

To adopt this for animation and easing etc, take the couple of lines from the slider change event and stick them inside the frame callback, manipulating the maths to suit your case.

// Set up the canvas / stage
var stage = new Konva.Stage({container: 'container1', width: 320, height: 180});

// Add a layer
var layer = new Konva.Layer({draggable: false});
stage.add(layer);

// show where the start of the path is.
var circle = new Konva.Circle({
  x: 66,
  y: 15,
  radius: 5,
  stroke: 'red'
 })
 layer.add(circle);

// draw a path.
    var path = new Konva.Path({
      x: 0,
      y: 0,
      data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
      stroke: 'green'
    });

// get the path length and set this as the dash and dashOffset. 
var pathLen = path.getLength();
path.dashOffset(pathLen);
path.dash([pathLen]);

layer.add(path)
stage.draw();

// Some UI bits
$('#dist').attr('max', parseInt(pathLen)); // set slider max to length of path
$('#pathLen').html('Path : ' + pathLen); // display path length

// jquery event listener on slider change
$('#dist').on('input', function(){

  // compute the new dash lenth as original path length - current slider value. 
  // Means that dashLen initially = path len and moves toward zero as slider val increases.
  var dashLen = pathLen - $(this).val();;
  path.dashOffset(dashLen);   // set new value
  layer.draw();               // refresh the layer to see effect

  // update the UI elements      
  $('#dashLen').html('Dash: ' + dashLen);
  $('#pathPC').html(parseInt(100-(100 * (dashLen/pathLen)), 10) + '%');

})

.info 
{
padding-left: 20px;

}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
<div class="slidecontainer">
  <input class="slider" id='dist' type="range" min="0" max="100" value="0" class="slider" id="myRange"/>
  <span class="info" id='pathPC'></span>
  <span class="info" id='pathLen'></span>
  <span class="info" id='dashLen'></span>
</div>
<div id='container1' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
<div id='img'></div>

这篇关于在 HTML5 画布上动画绘制路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13