在滑块的移动上画一条线
draw a line on movement of slider
我正在绘制滑块运动的弧线。如果你移动滑块,弧线将被绘制。旁边的弧与画布id画布我已经画了一条线。我试着在滑块的移动上画这条线。因此,随着滑块移动,线条将被绘制。目前只发生在arc上。任何帮助。请
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CH5EX10: Moving In A Simple Geometric Spiral </title>
<style>
.wrapper {
margin: 0 auto;
width: 1000px;
}
.uppleft {
float: left;
width: 1000px;
position: relative;
margin: 0 0 500px 10px;
}
.dnleft {
float: left;
width: 1000px;
}
.clear {
clear: both;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="uppleft">
<canvas id="canvasOne" width="300" height="300"width="500" height="500" style="position:absolute; left:5px; top:10px; border:1px solid red;">
Your browser does not support HTML5 Canvas.
</canvas>
<canvas id="canvasTwo" width="300" height="300" style="position:absolute; left:250px; top:30px; border:1px solid red;">
Your browser does not support HTML5 Canvas.
</canvas>
</div>
<div class="clear"></div>
<div class="dnleft">
<input id="slide1" type="range" min="0" max="100" step="1" value="0" onchange="counterSliderNew('slide1', '100');" />
</div>
</div>
</body>
<script type="text/javascript">
drawSlopeCurve1('canvasTwo')
function counterSliderNew(sID, maxValue) {
var slideVal = document.getElementById(sID).value;
//alert(slideVal);
if (maxValue == 100) {
slideVal = slideVal / 100;
}
var position = slideVal;
var startPt = {
x : 18.8,
y : 45
};
var controlPt = {
x : 28,
y : 160
};
var endPt = {
x : 228,
y : 165
};
if (slideVal == 0) {
} else if (slideVal > 0 && slideVal <= 34) {
erase('canvasOne');
drawBezier2('canvasOne', new Array({
x : 18.8,
y : 75
}, {
x : 28,
y : 160
}, {
x : 228,
y : 165
}), slideVal);
} else if (slideVal > 34 && slideVal <= 67) {
//alert(slideVal);
erase('canvasOne');
drawBezier2('canvasOne', new Array({
x : 18.8,
y : 75
}, {
x : 28,
y : 160
}, {
x : 228,
y : 165
}), slideVal);
} else if (slideVal > 67 && slideVal <= 100) {
erase('canvasOne');
drawBezier4('canvasOne', new Array({
x : 16,
y : 170
}, {
x : 160,
y : 72
}), slideVal);
//drawBezier3('canvasTwo', startPt, controlPt, endPt, position);
}
}
function drawBezier2(canId, points, slideVal) {
var canvas = document.getElementById(canId);
var context = canvas.getContext("2d");
// Draw guides
context.lineWidth = 2;
context.strokeStyle = "rgb(113, 113, 213)";
context.beginPath();
// Label end points
context.fillStyle = "rgb(0, 0, 0)";
// Draw spline segemnts
context.moveTo(points[0].x, points[0].y);
for (var t = 0; t <= slideVal; t += 0.1) {
context.lineTo(Math.pow(1 - t, 2) * points[0].x + 2 * (1 - t) * t * points[1].x + Math.pow(t, 2) * points[2].x, Math.pow(1 - t, 2) * points[0].y + 2 * (1 - t) * t * points[1].y + Math.pow(t, 2) * points[2].y);
}
// Stroke path
context.stroke();
}
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;
}
function drawSlopeCurve1(canId) {
var canvas = document.getElementById(canId);
var context = canvas.getContext('2d');
context.beginPath();
context.moveTo(16, 170);
context.lineTo(160, 72);
context.lineWidth = 0.6;
context.stroke();
}
</script>
</html>
像这样更新你的slide1输入:
<input id="slide1" type="range" min="0" max="100" step="1" value="0" onchange="counterSliderNew('slide1', '100'); drawSlopeCurve1('slide1',100);" />
像这样更新drawSlopeCurve1函数:
function drawSlopeCurve1(sID,maxValue) {
erase('canvasTwo');
var canId = 'canvasTwo';
var slideVal = parseInt(document.getElementById(sID).value);
var canvas = document.getElementById(canId);
var context = canvas.getContext('2d');
//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();
}
(你可以输入任何你想要的值为行结束点x1,y1和x2,y2)
实例
相关文章:
- 在地图上画一条路线
- 在d3中拖动一条线
- 我确实有一个表单,如果用户输入了输入,它应该检查否定或空的输入框,并抛出一条警告消息
- 我正在创建一个聊天,但每次我发送消息时,它都不会让我再发送另一条消息,就像表格一样;不起作用
- 当我的条件为true时,显示一条弹出消息
- 如何在javascript中的if或else块中运行一条jsp语句
- 加载页面每10秒显示一条不同的消息
- 更改路线时,在react.js中得到一条非常crypric的消息
- 当用户单击保存按钮时,标签会显示一条消息
- 将滚动条移动到页面中间的控制位置
- 仅显示一条通用消息,而不是每个输入显示一条
- 如何使用 JavaScript 函数在 JSP 中使用按钮移动到数据库的最后一条记录
- 如何使另一条线在动态图中向前移动
- 我可以使用旋转功能绕时钟移动一条线吗
- 在导航栏下流畅地移动一条线
- 画一条移动的线/曲线的最好方法是什么?
- 在移动的物体之间画一条线
- 在滑块的移动上画一条线
- dblclick的事件监听器导致鼠标移动的事件不起作用,并显示一个带有一条线的圆圈
- 如何在html5中连续移动一条线