1. <small id='cUOtG'></small><noframes id='cUOtG'>

      <tfoot id='cUOtG'></tfoot>
      <i id='cUOtG'><tr id='cUOtG'><dt id='cUOtG'><q id='cUOtG'><span id='cUOtG'><b id='cUOtG'><form id='cUOtG'><ins id='cUOtG'></ins><ul id='cUOtG'></ul><sub id='cUOtG'></sub></form><legend id='cUOtG'></legend><bdo id='cUOtG'><pre id='cUOtG'><center id='cUOtG'></center></pre></bdo></b><th id='cUOtG'></th></span></q></dt></tr></i><div id='cUOtG'><tfoot id='cUOtG'></tfoot><dl id='cUOtG'><fieldset id='cUOtG'></fieldset></dl></div>

      • <bdo id='cUOtG'></bdo><ul id='cUOtG'></ul>
      <legend id='cUOtG'><style id='cUOtG'><dir id='cUOtG'><q id='cUOtG'></q></dir></style></legend>
    2. 如何绘制一条可以用画布向左移动的曲线?

      How to draw a curve that could move to left with canvas?(如何绘制一条可以用画布向左移动的曲线?)
      <i id='7nZro'><tr id='7nZro'><dt id='7nZro'><q id='7nZro'><span id='7nZro'><b id='7nZro'><form id='7nZro'><ins id='7nZro'></ins><ul id='7nZro'></ul><sub id='7nZro'></sub></form><legend id='7nZro'></legend><bdo id='7nZro'><pre id='7nZro'><center id='7nZro'></center></pre></bdo></b><th id='7nZro'></th></span></q></dt></tr></i><div id='7nZro'><tfoot id='7nZro'></tfoot><dl id='7nZro'><fieldset id='7nZro'></fieldset></dl></div>
      <tfoot id='7nZro'></tfoot>

        1. <legend id='7nZro'><style id='7nZro'><dir id='7nZro'><q id='7nZro'></q></dir></style></legend>

          <small id='7nZro'></small><noframes id='7nZro'>

            <tbody id='7nZro'></tbody>

              <bdo id='7nZro'></bdo><ul id='7nZro'></ul>
              • 本文介绍了如何绘制一条可以用画布向左移动的曲线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                I'm writing a program that will draw the sine curve with canvas.
                HTML:

                <canvas id="mycanvas" width="1000" height="100">
                    Your browser is not supported.
                </canvas>
                

                JavaScript:

                var canvas = document.getElementById("mycanvas");
                if (canvas.getContext) {
                    var ctx = canvas.getContext("2d");
                    ctx.lineWidth = 3;
                    var x = 0,
                        y = 0;
                    var timeout = setInterval(function() {
                        ctx.beginPath();
                        ctx.moveTo(x, y);
                        x += 1;
                        y = 50 * Math.sin(0.1 * x) + 50;
                        ctx.lineTo(x, y);
                        ctx.stroke();
                        if (x > 1000) {
                            clearInterval(timeout);
                        }
                    }, 10);
                }
                

                This works really nice: http://jsfiddle.net/HhGnb/

                However, now I can only offer say 100px for the canvas width, so only the leftest 100px of the curve could be seen. http://jsfiddle.net/veEyM/1/
                I want to archive this effect: when the right point of the curve is bigger than the width of canvas, the whole curve could move left, so I can see the rightest point of the curve, it's a bit like the curve is flowing to left. Can I do that?

                解决方案

                One of the basic ideas of the <canvas> element is that the computer 'forgets' the drawing commands and only saves the pixels, like a bitmap. So to move everything to the left, you need to clear the canvas and draw everything again.

                There is also one thing I'd like to advise you - you always start with x = 0 and y = 0, but obviously at x = 0 then y is not necessarily equal to 0 as well. EDIT: implemented this.

                Anyway, I ended up with this code: http://jsfiddle.net/veEyM/5/

                var canvas = document.getElementById("mycanvas");
                var points = {}; // Keep track of the points in an object with key = x, value = y
                var counter = 0; // Keep track when the moving code should start
                
                function f(x) {
                    return 50 * Math.sin(0.1 * x) + 50;
                }
                
                if (canvas.getContext) {
                    var ctx = canvas.getContext("2d");
                    ctx.lineWidth = 3;
                    var x = 0,
                        y = f(0);
                    var timeout = setInterval(function() {
                        if(counter < 100) { // If it doesn't need to move, draw like you already do
                            ctx.beginPath();
                            ctx.moveTo(x, y);
                            points[x] = y;
                            x += 1;
                            y = f(x);
                            ctx.lineTo(x, y);
                            ctx.stroke();
                            if (x > 1000) {
                                clearInterval(timeout);
                            }
                        } else { // The moving part...
                            ctx.clearRect(0, 0, 100, 100); // Clear the canvas
                            ctx.beginPath();
                            points[x] = y;
                            x += 1;
                            y = f(x);
                            for(var i = 0; i < 100; i++) {
                                // Draw all lines through points, starting at x = i + ( counter - 100 )
                                // to x = counter. Note that the x in the canvas is just i here, ranging
                                // from 0 to 100
                                ctx.lineTo(i, points[i + counter - 100]);
                            }
                            ctx.stroke();
                        }
                        counter++;
                    }, 10);
                }
                

                这篇关于如何绘制一条可以用画布向左移动的曲线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How does object-fit work with canvas element?(对象适合如何与画布元素一起使用?)
                How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)(如何在 HTML5(或 Fabric.js)中制作屋顶文字效果和山谷文字效果)
                Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)
                What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)
                  <tbody id='B9uGw'></tbody>

                    • <i id='B9uGw'><tr id='B9uGw'><dt id='B9uGw'><q id='B9uGw'><span id='B9uGw'><b id='B9uGw'><form id='B9uGw'><ins id='B9uGw'></ins><ul id='B9uGw'></ul><sub id='B9uGw'></sub></form><legend id='B9uGw'></legend><bdo id='B9uGw'><pre id='B9uGw'><center id='B9uGw'></center></pre></bdo></b><th id='B9uGw'></th></span></q></dt></tr></i><div id='B9uGw'><tfoot id='B9uGw'></tfoot><dl id='B9uGw'><fieldset id='B9uGw'></fieldset></dl></div>

                          <bdo id='B9uGw'></bdo><ul id='B9uGw'></ul>
                          <legend id='B9uGw'><style id='B9uGw'><dir id='B9uGw'><q id='B9uGw'></q></dir></style></legend>

                          <small id='B9uGw'></small><noframes id='B9uGw'>

                          <tfoot id='B9uGw'></tfoot>