html 实时曲线 js,javascript – 滑块移动时的html5-canvas绘图曲线

嗨,我想在移动滑块时绘制曲线.我有两种曲线.贝塞尔曲线和二次曲线.当我绘制曲线时,我想在同一条路径上移动一个对象.我希望它是动态的,所以如果我能够改变曲线点.

所以我需要一个函数,我可以调用滑块更改,因为我正在为行做.

这是我的代码.此代码仅适用于chrome.

.wrapper {

margin: 0 auto;

width: 1000px;

}

.canHdr {

float: left;

width: 450px;

height: 400px;

border: 1px solid red;

}

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.

[No canvas support]

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.

[No canvas support]

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.

[No canvas support]

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.

[No canvas support]

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);

}

解决方法:

您应该首先在函数中定义曲线,以便计算每个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);

}

添加了html5slider.js以使滑块在firefox中工作.

标签:javascript,html5-canvas

来源: https://codeday.me/bug/20190709/1409634.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值