我相信你正在寻找这样的东西:
draw(0);
$('#range').on('change', function(){
range = parseInt($(this).val());
draw(range)
})
function draw(val){
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d'),
x = 100,
y = 50,
d;
context.clearRect(0, 0, canvas.width, canvas.height);
d = Math.sqrt(Math.pow(val,2) + Math.pow(50,2));
context.beginPath();
context.lineWidth = 1;
context.arc(x,y+val,d,0,2*Math.PI);
// line color
context.strokeStyle = 'black';
context.stroke();
// Cut off the top of the circle.
context.clearRect(0, 0, canvas.width, y);
// This stuff's just to show some dots
context.fillStyle = 'red';
context.fillRect(x-1,y-1,2,2); // Middle
context.fillRect(x-52,y-2,4,4);//Target point 1
context.fillRect(x+48,y-2,4,4);// Target point 2
context.fillRect(x-2,y+d+val-2,4,4); // Point on circle
context.fillStyle = 'black';
}
这个方法有一些缺点,当你越靠近圆圈时,它会变得“慢” ,因为圆圈在隐藏部分呈指数级增长(滑块控制其大小),而且目前它不能用于对角线。
除此之外,它的表现如预期一样。
请查看:http://jsfiddle.net/KZBzq/4/
使用 bezierCurveTo 更新答案。
HTML
<label>Range :</label>
<input type="range" name="points" value="0" min="0" step="1" max="100" id="range">
<canvas id="myCanvas" width="578" height="250"></canvas>
JS
draw(100);
$('#range').on('change', function(){
range = parseInt($(this).val());
draw(100-range)
})
function draw(val){
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d'),
x = 100,
y = 100,
cp1x = x/2,
cp2x = y/2,
cp1y = val,
cp2y = val;
context.clearRect(0, 0, canvas.width, canvas.height);
context.beginPath();
context.lineWidth = 1;
context.moveTo(25 , x);
context.bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y);
// line color
context.strokeStyle = 'black';
context.stroke();
}