为什么我的画布线条没有单独绘制
Why are my canvas lines not being drawn individually?
我的画布上有几条动画线,它们不是单独绘制的,而是试图绘制成一条完整的路径。为什么会发生这种情况?
$(document).ready(function(){
canvas = document.getElementById("test");
ctx = canvas.getContext("2d");
function animateLines(name, x1, y1, x2, y2, stroke, width, duration){
var count = 0;
var ms = 10;
duration = duration * ms;
ctx.beginPath();
ctx.moveTo(x1, y1);
function countNumbers(){
count += 1;
if(x2 > x1){
ctx.lineTo((x1 + count), y2);
}
else if(y2 > y1){
ctx.lineTo(x1, (y1 + count));
}
if((x1 < x2) && (count == x2)){
clearInterval(counter);
}
else if((y1 < y2) && (count == y2)){
clearInterval(counter);
}
ctx.lineWidth = width;
ctx.strokeStyle = stroke;
ctx.stroke();
}
$("#pause").on("click", function(){
clearInterval(counter);
})
$("#play").on("click", function(){
counter = setInterval(countNumbers, duration);
})
}
animateLines("Line", 0, 100, 100, 100, "white", 5, 3);
//animateLines("Line2", 150, 250, 350, 250, "red", 5, 5);
//animateLines("Line3", 100, 0, 100, 300, "blue", 5, 1);
})
当我只调用一次函数时,它工作得很好,当我多次调用它时,它会试图绘制一个完整的形状。
您需要为每一行moveTo(x1, x2)
,也需要为beginPath()
来更改其笔划颜色:否则,stroke()
将绘制所有形状。
var canvas = document.getElementById("test"),
ctx = canvas.getContext("2d");
function animateLines(name, x1, y1, x2, y2, stroke, width, duration) {
var count = 0;
var ms = 10;
duration = duration * ms;
var counter;
function countNumbers() {
ctx.beginPath();
ctx.moveTo(x1, y1);
count += 1;
if (x2 > x1) {
ctx.lineTo((x1 + count), y2);
} else if (y2 > y1) {
ctx.lineTo(x1, (y1 + count));
}
if ((x1 < x2) && (count == x2)) {
clearInterval(counter);
} else if ((y1 < y2) && (count == y2)) {
clearInterval(counter);
}
ctx.lineWidth = width;
ctx.strokeStyle = stroke;
ctx.stroke();
}
$("#pause").on("click", function() {
clearInterval(counter);
})
$("#play").on("click", function() {
counter = setInterval(countNumbers, duration);
})
}
animateLines("Line", 0, 100, 100, 100, "green", 5, 3);
animateLines("Line2", 150, 250, 350, 250, "red", 5, 5);
animateLines("Line3", 100, 0, 100, 300, "blue", 5, 1);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<button id="pause">Pause</button>
<button id="play">play</button></div>
<canvas id="test" width="350" height="350"></canvas>
相关文章:
- D3在一个调用中绘制不同的SVG形状,没有可见性
- 有没有一个javascript图形绘制库可以进行气球树布局
- javascript弹出窗口没有正确加载html表单
- 如果用户没有更改,则表单值为空
- 引导菜单没有't在导航栏中单击打开
- 在没有焦点的情况下捕获画布上的第一次单击事件
- 为什么我的画布线条没有单独绘制
- d3.js yaxis没有正确绘制所有记号
- 未捕获的类型错误:对象 [对象对象] 没有方法“绘制”游戏.js:202 绘制
- JavaScript有没有办法绘制kml或gpx数据
- 对于动态创建的元素,在没有单击的情况下在执行时调用
- D3.js中的折线图没有正确绘制
- 当没有单击某个元素时,我如何在鼠标下执行操作
- 箭头没有正确绘制在fabricJS画布上
- 如果没有单击复选框,如何禁用按钮
- 谷歌地图没有正确绘制,每当数晚& &;朗点是巨大的
- 如果没有单击一组元素,如何触发事件
- 如果没有单击Bootstrap选项卡,如何在jQuery中进行检查
- 无法从JSON字符串实例化类型的值;没有单字符串构造函数/工厂方法
- 有没有办法绘制具有相对定位的画布图像