【问题标题】:html5-canvas drawing curve on move of sliderhtml5-canvas 在滑块移动时绘制曲线
【发布时间】:2013-06-01 07:38:21
【问题描述】:

您好,我想在移动滑块时画一条曲线。我有两种曲线。贝塞尔曲线和二次曲线。此外,当我绘制曲线时,我想在同一条路径上移动一个对象。我希望它是动态的,所以如果我应该能够改变曲线点。 所以我需要一个函数,我可以像对 line 所做的那样调用滑块更改。 这是我的代码。此代码仅适用于 chrome。

            <!DOCTYPE HTML>
            <html>
                <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
                    <meta name="viewport" content="width=device-width, initial-scale=1">

                    <style type="text/css">
                        .wrapper {
                            margin: 0 auto;
                            width: 1000px;
                        }
                        .canHdr {
                            float: left;
                            width: 450px;
                            height: 400px;
                            border: 1px solid red;
                        }
                    </style>

                </head>
                <body>
                    <form>
                        <!-- wrapper -->
                        <div class="wrapper">

                            <!-- canHdr -->
                            <div id="canHdr" class="canHdr" >

                                <p>
                                    This is my 1st div with quadratic curve I want to draw this curve as I move the slider. I want to make it dynamic so when I should be able to change the curve points. Also I want to move an object on that curve as I am doing in my 3rd div.
                                </p>

                                <div class="canOuterHdr" >
                                    <canvas id="myCanvas1" width="300" height="195" style="position: relative;">
                                        [No canvas support]
                                    </canvas>

                                </div>

                                <div id="slider1" class="newBg">
                                    <input id="slide1" type="range" min="0" max="100" step="1" value="0" onchange="counterSlider('slide1');" />
                                </div>

                            </div>
                            <!--/ canHdr -->
                            <!-- canHdr2 -->
                            <div id="canHdr2" class="canHdr" >

                                <p>
                                    This is my 2nd div. I have bezier curve. I want to make it dynamic so when I should be able to change the curve points. Also I want to move an object on that curve as I am doing in my 3rd div.
                                </p>

                                <div class="canOuterHdr" >
                                    <canvas id="myCanvas2" width="300" height="195" style="position: relative;">
                                        [No canvas support]
                                    </canvas>

                                </div>

                                <div id="slider2" class="newBg">
                                    <input id="slide2" type="range" min="0" max="100" step="1" value="0" onchange="counterSlider('slide2');" />
                                </div>

                            </div>
                            <!-- canHdr2 -->
                            <!-- canHdr3 -->
                            <div id="canHdr3" class="canHdr" >
                                <p>
                                    This is my 3rd div with slanting line. I want to move a ball on this line when I move the slider. So as the line increases ball will also move on the line.
                                </p>

                                <div class="canOuterHdr" >
                                    <canvas id="myCanvas3" width="300" height="195" style="position: relative;">
                                        [No canvas support]
                                    </canvas>

                                </div>

                                <div id="slider3" class="newBg">
                                    <input id="slide3" type="range" min="0" max="100" step="1" value="0" onchange=" drawSlopeCurve2('slide3','100'); " />
                                </div>

                            </div>
                            <!--/ canHdr3 -->
                            <!-- canHdr4 -->
                            <div id="canHdr4" class="canHdr" >

                                <p>
                                    This is my 4th div with slanting line. I want to move a ball on this line when I move the slider. So as the line increases ball will also move on the line.
                                </p>

                                <div class="canOuterHdr" >
                                    <canvas id="myCanvas4" width="300" height="195" style="position: relative;">
                                        [No canvas support]
                                    </canvas>

                                </div>

                                <div id="slider4" class="newBg">
                                    <input id="slide4" type="range" min="0" max="100" step="1" value="0" onchange=" drawSlopeCurve1('slide4','100'); " />
                                </div>

                            </div>
                            <!--/ canHdr4 -->

                        </div>
                        <!-- /wrapper -->

                        <script type="text/javascript">
                            newSprite('myCanvas3', 16, 170);
                            quadraticCurve('myCanvas1', 18.8, 45, 28, 160, 228, 165);
                            bezierCurve('myCanvas2', 20, 75, 55.2, 150.0, 200,100, 228, 165)
                            function counterSlider(sID) {

                                var slideVal = document.getElementById(sID).value;
                                /*if (maxValue ==100){

                                 slideVal=slideVal/100;
                                 }*/
                                slideVal = slideVal / 100;

                                if (slideVal == 0) {

                                /*  erase('myCanvas2');
                                    erase('myCanvas3');
                                    erase('myCanvas4');*/
                                    //newSprite('myCanvas1b', 18.8, 45);
                                    newSprite('myCanvas3', 16, 170);

                                } else if (slideVal > 0 && slideVal <= 34) {

                                    /*erase('myCanvas1');
                                    //erase('myCanvas1b');
                                    erase('myCanvas2');
                                    erase('myCanvas3');
                                    erase('myCanvas4');*/

                                } else if (slideVal > 34 && slideVal <= 67) {

                                    /*erase('myCanvas1');

                                    erase('myCanvas2');
                                    erase('myCanvas3');
                                    erase('myCanvas4');*/

                                } else if (slideVal > 67 && slideVal <= 100) {

                                    /*erase('myCanvas1');

                                    erase('myCanvas2');
                                    erase('myCanvas3');
                                    erase('myCanvas4');*/

                                }
                            }

                            function erase(canvasId) {

                                var canvas = document.getElementById(canvasId);
                                var context = canvas.getContext("2d");
                                context.beginPath();
                                context.clearRect(0, 0, canvas.width, canvas.height);
                                canvas.width = canvas.width;

                            }

                            /**********for backgroundImage********************/

                            function quadraticCurve(canId, spx, spy, cpx, cpy, endx, endy) {

                                var canvas = document.getElementById(canId);
                                var ctx = canvas.getContext('2d');

                                ctx.beginPath();
                                ctx.moveTo(spx, spy);
                                ctx.quadraticCurveTo(cpx, cpy, endx, endy);
                                ctx.strokeStyle = "#eaca2d";
                                ctx.stroke();

                            }

                            function bezierCurve(canId, spx, spy, cpx1, cpy1, cpx2, cpy2, endx, endy) {

                                var canvas = document.getElementById(canId);
                                var ctx = canvas.getContext('2d');

                                ctx.beginPath();
                                ctx.moveTo(spx, spy);
                                ctx.quadraticCurveTo(cpx1, cpy1, cpx2, cpy2, endx, endy);
                                ctx.strokeStyle = "#eaca2d";
                                ctx.stroke();

                            }

                            function newSprite(canId, mvx, mvy) {
                                var canvas = document.getElementById(canId);
                                var ctx = canvas.getContext('2d');
                                ctx.globalCompositeOperation = 'source-over';
                                //ctx.globalCompositeOperation = "destination-over";
                                ctx.beginPath();
                                ctx.fillStyle = "#0077c1";
                                ctx.arc(mvx, mvy, 6, 0, Math.PI * 2, true);
                                ctx.closePath();
                                ctx.fill();
                            }

                            function drawSlopeCurve1(sID, maxValue) {
                                // erase('canvasTwo');

                                var canId = 'myCanvas4';
                                var slideVal = parseInt(document.getElementById(sID).value);
                                var canvas = document.getElementById(canId);
                                var context = canvas.getContext('2d');
                                canvas.width = canvas.width;
                                //line end points
                                x1 = 16;
                                y1 = 170;
                                x2 = 200;
                                y2 = 80;

                                //get slope (rise over run)
                                var m = (y2 - y1) / (x2 - x1);
                                //get y-intercept
                                var b = y1 - (m * x1);
                                //get distance between the two points
                                var distance = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
                                //get new x and y values
                                var x = x1 + parseInt(distance / maxValue * slideVal);
                                var y = parseInt(m * x + b);

                                context.beginPath();
                                context.moveTo(x1, y1);
                                context.lineTo(x, y);
                                context.lineWidth = 0.6;
                                context.stroke();

                                newSprite(canId, x, y);
                            }

                            function drawSlopeCurve2(sID, maxValue) {
                                // erase('canvasTwo');

                                var canId = 'myCanvas3';
                                var slideVal = parseInt(document.getElementById(sID).value);
                                var canvas = document.getElementById(canId);
                                var context = canvas.getContext('2d');
                                canvas.width = canvas.width;
                                //line end points
                                x1 = 16;
                                y1 = 170;
                                x2 = 160;
                                y2 = 72;

                                //get slope (rise over run)
                                var m = (y2 - y1) / (x2 - x1);
                                //get y-intercept
                                var b = y1 - (m * x1);
                                //get distance between the two points
                                var distance = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
                                //get new x and y values
                                var x = x1 + parseInt(distance / maxValue * slideVal);
                                var y = parseInt(m * x + b);

                                context.beginPath();
                                context.moveTo(x1, y1);
                                context.lineTo(x, y);
                                context.lineWidth = 0.6;
                                context.stroke();

                                newSprite(canId, x, y);
                            }

                        </script>
                    </form>
                </body>
            </html>

这是 jsfiddle 的链接:http://jsfiddle.net/QRVxH/

【问题讨论】:

  • 你试过什么?您已经有了绘制曲线的代码,所以只需在滑块更改时更改值,您已经编写了滑块值更改的处理程序。

标签: javascript html5-canvas


【解决方案1】:

您应该从在函数中定义曲线开始,这样您就可以计算每个 x 位置的图形应该在哪里结束。然后你也知道在哪里画结束点。

我的演示函数有一个在画布上绘制正弦的函数:

function calc(x){
    var y = 100 + ( 50*Math.sin(x*400));
    return y;
}

通过在点之间画一条线(不是曲线!),您可以获得图表。

for (var x=0; x<=400 && (x <= slider.value); x+=3){        
    y= calc(x);
    ctx.lineTo(x, y);
}

工作演示:http://jsfiddle.net/w1ll3m/CbjWK/11/

添加html5slider.js 以使滑块在Firefox 中工作。

【讨论】:

  • 非常感谢您的帮助。我有绘制曲线的代码。但它并不像你的那样顺利绘制。任何人都可以帮助使其顺利运行吗?我也想在同一条曲线上移动一个对象。最后一个问题,我怎样才能让这个工作在贝塞尔曲线中。我的工作 js 小提琴链接:jsfiddle.net/JGz4T/1
  • 您的代码有多个路径(if/else 带有点集),这并不容易!用y=3x^2y=cos(x) 之类的等式定义你的线,这样你就可以简单地选择你的x 来计算你的y。这样,您可以以 1 到 4 个像素(x 轴)的步长绘制一个点。 jsfiddle.net/w1ll3m/JGz4T/4看我的红线!
  • 你能解释一下你用来获得分数的方程式吗?我也试图在 0 位置启动滑块,但它会到达结束位置。
  • 这个等式是简单的数学运算,请参阅mathsisfun.com/data/grapher-equation.html 并尝试一些示例。我只是取了 x 的平方根并调整了函数,直到它与您的线路基本匹配。我在答案中编辑了 jsfiddle,并以更多行为例。
猜你喜欢
  • 2019-02-28
  • 1970-01-01
  • 2017-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
相关资源
最近更新 更多