我在动力学框架中使用帆布或楔形来加载2个圆形图像,1个用于图像背景,1个用于图像百分比的饼图。我为canvas容器绑定了touchstart,touchmove,touchend事件。然后我面临2个问题:第一件事如何使用画布加载一部分圆形图像。例如:只需加载1/4的图像圈。第二件事是如何根据触摸点计算馅饼的部分,当我们接触并移动手指的馅饼百分比也会增加和减少。
请给我一些建议或例子。提前谢谢
答案 0 :(得分:3)
如何绘制包含基于点击次数/点击而变化的图像的楔形
对于内部“wedge”,您可以使用Kinetic.Group对象并使用clipFunc将图像裁剪为楔形
这是image-wedge的clipFunc:
clipFunc: function(canvas) {
var ctx = canvas.getContext();
var w=wedgeWidth;
var h=wedgeHeight;
var r=wedgeRadius;
var angle1=0; // start at vertical
var angle2=angle1+radianAngle;
ctx.moveTo(cx,cy);
ctx.arc(cx,cy,r,angle1,angle2,false);
ctx.closePath();
}
radianAngle是控制楔形大小的变量。
您可以像这样设置radianAngle(和楔形尺寸):
// if the user clicks/taps on the stage
// redraw the image-wedge to the angle of the click/tap
function setRadianAngle(touchX,touchY){
radianAngle=(Math.atan2(touchY-cy,touchX-cx)+Math.PI*2)%(Math.PI*2);
layer.draw();
}
这是代码和小提琴:http://jsfiddle.net/m1erickson/DxZLJ/
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Prototype</title>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.5.4.min.js"></script>
<style>
#container{
border:solid 1px #ccc;
margin-top: 10px;
width:400px;
height:400px;
}
</style>
<script>
$(function(){
var stage = new Kinetic.Stage({
container: 'container',
width: 400,
height: 400
});
var layer = new Kinetic.Layer();
stage.add(layer);
enableStageClick();
function enableStageClick(){
$(stage.getContent()).on('click tap', function (event) {
var pos=stage.getMousePosition();
var mouseX=parseInt(pos.x);
var mouseY=parseInt(pos.y);
console.log(mouseX+"/"+mouseY);
setRadianAngle(mouseX,mouseY);
});
}
var wedgeWidth=300;
var wedgeHeight=300;
var wedgeRadius=100;
var borderRadius=20;
var radianAngle=135*Math.PI/180;
var cx=wedgeWidth/2;
var cy=wedgeHeight/2;
// create the outside "border" circle
var border = new Kinetic.Circle({
x: wedgeWidth/2,
y: wedgeHeight/2,
radius: wedgeRadius+borderRadius,
fill:"skyblue",
stroke: "blue",
strokeWidth: 5
});
layer.add(border);
// create the inside "image-wedge" group
var wedge=new Kinetic.Group({
x:0,
y:0,
width:wedgeWidth,
height:wedgeHeight,
// clip to a wedge with angle==degreeAngle off vertical
clipFunc: function(canvas) {
var ctx = canvas.getContext();
var w=wedgeWidth;
var h=wedgeHeight;
var r=wedgeRadius;
var angle1=0; // start at vertical
var angle2=angle1+radianAngle;
ctx.moveTo(cx,cy);
ctx.arc(cx,cy,r,angle1,angle2,false);
ctx.closePath();
}
});
layer.add(wedge);
// create the image that will be clipped inside the wedge
var image=new Kinetic.Image({
image:image,
x:0,
y:0,
width:wedgeWidth,
height:wedgeHeight,
});
wedge.add(image);
// load the image for the Kinetic Image object
var img=new Image();
img.onload=function(){
image.setImage(img);
layer.draw();
}
img.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/KoolAidMan.png";
// if the user clicks/taps on the stage
// redraw the image-wedge to the angle of the click/tap
function setRadianAngle(touchX,touchY){
radianAngle=(Math.atan2(touchY-cy,touchX-cx)+Math.PI*2)%(Math.PI*2);
layer.draw();
}
}); // end $(function(){});
</script>
</head>
<body>
<p>Click/Touch to change angle of wedge-image</p>
<div id="container"></div>
</body>
</html>