Javascript 如何编写一个循环,以便我将绘制到用户画布?

Javascript 如何编写一个循环,以便我将绘制到用户画布?,javascript,html,canvas,Javascript,Html,Canvas,我有一个画布元素。我有一些麻烦,如何在“实时”中绘制用户画布,。。所以,当他们打开网站的时候,我的画并不在那里,而是画在画布上,就像有人在画一样。。。所以在坐标中循环 到目前为止我都是这么做的,但这是巴德!它的绘图速度很慢,需要大量的CPU // Pencil Points var ppts = []; /* Drawing on Paint App */ tmp_ctx.lineWidth = 4; tmp_ctx.lineJoin = 'round'; tmp_ctx.lineC

我有一个画布元素。我有一些麻烦,如何在“实时”中绘制用户画布,。。所以,当他们打开网站的时候,我的画并不在那里,而是画在画布上,就像有人在画一样。。。所以在坐标中循环

到目前为止我都是这么做的,但这是巴德!它的绘图速度很慢,需要大量的CPU

    // Pencil Points
var ppts = [];

/* Drawing on Paint App */

tmp_ctx.lineWidth = 4;
tmp_ctx.lineJoin = 'round';
tmp_ctx.lineCap = 'round';
tmp_ctx.strokeStyle = '#4684F6';
tmp_ctx.fillStyle = '#4684F6';

// Tmp canvas is always cleared up before drawing.
tmp_ctx.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height);
tmp_ctx.beginPath();

var timer = 0;

$.timer(500, function() {
    ppts.push({x: 10*timer, y: 5*timer});
    timer++;
})

$.timer(10, function() {
    if (timer > 250) {
        timer = 0;
        clearTempCanvas();
    } else {

        for (var i = 1; i < ppts.length - 2; i++) {
            var c = (ppts[i].x + ppts[i + 1].x) / 2;
            var d = (ppts[i].y + ppts[i + 1].y) / 2;

            tmp_ctx.quadraticCurveTo(ppts[i].x, ppts[i].y, c, d);
        }

        console.log(i);


        tmp_ctx.stroke();
    }
})

function clearTempCanvas() {
    // Writing down to real canvas now
    ctx.drawImage(tmp_canvas, 0, 0);
    // Clearing tmp canvas
    tmp_ctx.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height);
    // Emptying up Pencil Points
    ppts = [];  
}
//铅笔尖
var-ppts=[];
/*绘画应用程序*/
tmp_ctx.lineWidth=4;
tmp_ctx.lineJoin='圆形';
tmp_ctx.lineCap='圆形';
tmp_ctx.strokeStyle='#4684F6';
tmp_ctx.fillStyle='#4684F6';
//Tmp画布始终在绘制前清除。
tmp_ctx.clearRect(0,0,tmp_canvas.width,tmp_canvas.height);
tmp_ctx.beginPath();
var定时器=0;
$.timer(500,函数(){
推送({x:10*定时器,y:5*定时器});
定时器++;
})
$.timer(10,函数(){
如果(计时器>250){
定时器=0;
clearTempCanvas();
}否则{
对于(变量i=1;i
以下是一个供您学习的示例:

它的工作原理如下:

  • 定义一些要设置动画的点,并将这些点放入数组points.push({x:25,y:50})
  • 使用requestAnimationFrame创建动画循环
  • 将每条线段打断为100个子线段,并沿这些子线段设置动画
示例代码:

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" />
<script src="http://code.jquery.com/jquery.min.js"></script>

<style>
    body{ background-color: ivory; }
    canvas{border:1px solid red;}
</style>

<script>
    $(function(){

        var canvas=document.getElementById("canvas");
        var ctx=canvas.getContext("2d");
        ctx.lineWidth=2;
        ctx.strokeStyle="blue";

        var points=[];
        points.push({x:125,y:125});
        points.push({x:250,y:200});
        points.push({x:125,y:200});
        points.push({x:125,y:125});
        var pointIndex=1;
        var linePct=0;
        var continueAnimating=true;

        var img=new Image();img.onload=start;
        img.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/pen.png";
        function start(){
            animate();
        }

        function draw(pointIndex,linePct){

            // clear the canvas
            ctx.clearRect(0,0,canvas.width,canvas.height);

            // draw fully completed lines
            ctx.beginPath();
            ctx.moveTo(points[0].x,points[0].y);
            for(var i=1;i<pointIndex;i++){
                ctx.lineTo(points[i].x,points[i].y);
            }

            // draw current line-in-process
            var pos=getLineXYatPercent(points[pointIndex-1],points[pointIndex],linePct/100);
            ctx.lineTo(pos.x,pos.y);
            ctx.stroke();

            // draw the pen
            ctx.drawImage(img,pos.x-93,pos.y-92);
        }

        function animate() {

            if(!continueAnimating){return;}

            requestAnimationFrame(animate);

            // Drawing code goes here
            draw(pointIndex,linePct);

            if(++linePct>100){
                linePct=1;
                if(++pointIndex>points.length-1){
                    continueAnimating=false;
                }
            }
        }


        function getLineXYatPercent(startPt,endPt,percent) {
            var dx = endPt.x-startPt.x;
            var dy = endPt.y-startPt.y;
            var X = startPt.x + dx*percent;
            var Y = startPt.y + dy*percent;
            return( {x:X,y:Y} );
        }        

    }); // end $(function(){});
</script>

</head>

<body>
    <canvas id="canvas" width=350 height=350></canvas>
</body>
</html>

正文{背景色:象牙;}
画布{边框:1px纯红;}
$(函数(){
var canvas=document.getElementById(“canvas”);
var ctx=canvas.getContext(“2d”);
ctx.线宽=2;
ctx.strokeStyle=“蓝色”;
var点=[];
点.推力({x:125,y:125});
点.推力({x:250,y:200});
点.推力({x:125,y:200});
点.推力({x:125,y:125});
var pointIndex=1;
var-linePct=0;
var continueAnimating=true;
var img=new Image();img.onload=start;
img.src=”https://dl.dropboxusercontent.com/u/139992952/stackoverflow/pen.png";
函数start(){
制作动画();
}
函数绘图(点索引、线PCT){
//清理画布
clearRect(0,0,canvas.width,canvas.height);
//画完整的线条
ctx.beginPath();
移动到(点[0].x,点[0].y);
对于(变量i=1;i100){
linePct=1;
如果(++pointIndex>points.length-1){
continueAnimating=false;
}
}
}
函数getLineXYatPercent(开始、结束、百分比){
var dx=endPt.x-startPt.x;
变量dy=结束点y-开始点y;
var X=起始时间X+dx*百分比;
变量Y=起始时间Y+dy*百分比;
返回({x:x,y:y});
}        
}); // end$(函数(){});

所以当用户查看画布时,基本上你想要在画布上绘制动画?@NevinMadhukarK,是的,就是这样!不:(比我强。给我们这些笨蛋留下一个简单的问题:不,没关系。:)把答案拿回来,那个家伙一定是在头脑风暴。