我在没有动力学的情况下做到了这一点。这段代码按我的意愿工作。 --fiddle 没有 kineticjs。看一看
现在我想用 kineticjs 做同样的代码。这是我到目前为止所做的 --fiddle with kineticjs。
线条根本不动?我哪里错了?我花了一整天但无法弄清楚问题所在。
这是我的 javascript with kinetic.results 在小提琴中看不到,因为没有包含 kineticjs 的选项。虽然我已经把我的代码放在那里了。欢迎提供任何帮助。
var stage=new Kinetic.Stage({
container:'container',
width:500,
height:500
});
var layer=new Kinetic.Layer();
var bg=new Kinetic.Rect({
x:0,
y:0,
width:stage.getWidth(),
height:stage.getHeight(),
fill: 'antiquewhite'
});
layer.add(bg);
var drawHair = function(canvas) {
var context = canvas.getContext();
context.beginPath();
context.moveTo(this.attrs.sx, this.attrs.sy);
context.bezierCurveTo(this.attrs.cp1x, this.attrs.cp1y, this.attrs.cp2x, this.attrs.cp2y, this.attrs.endx, this.attrs.endy);
context.closePath();
canvas.fillStroke(this);
};
function Hair(a, b, c, d, e, f, g, h) {
return new Kinetic.Shape({
drawFunc: drawHair,
fill: '#000',
lineJoin: 'round',
stroke: 'grey',
strokeWidth: 8,
sx: 136 + a,//start position of curve.used in moveTo(sx,sy)
sy: 235 + b,
cp1x: 136 + c,//control point 1
cp1y: 222 + d,
cp2x: 136 + e,//control point 2
cp2y: 222 + f,
endx: 136 + g,//end points
endy: 210 + h
});
}
var hd =[];
function init(){//this function draws each hair/curve
hd.push(new Hair(0, 0, 0, 0, 0, 0, 0, 0));
layer.add(hd[0]);
hd.push(new Hair(15, 0, 15, 0, 15, 0, 15, 0));
layer.add(hd[1]);
hd.push(new Hair(30, 0, 30, 0, 30, 0, 30, 0));
layer.add(hd[2]);
hd.push(new Hair(45, 0, 45, 0, 45, 0, 45, 0));
layer.add(hd[3]);
hd.push(new Hair(60, 0, 60, 0, 60, 0, 60, 0));
layer.add(hd[4]);
hd.push(new Hair(75, 0, 75, 0, 75, 0, 75, 0));
layer.add(hd[5]);
hd.push(new Hair(90, 0, 90, 0, 90, 0, 90, 0));
layer.add(hd[6]);
hd.push(new Hair(105, 0, 105, 0, 105, 0, 105, 0));
layer.add(hd[7]);
hd.push(new Hair(120, 0, 120, 0, 120, 0, 120, 0));
layer.add(hd[8]);
stage.add(layer);
}
var bend1=0;
var bend2=0;
var bend3=0;
var bend4=0;
var bend5=0;
var bend6=0;
var bend7=0;
var bend8=0;
var bend9=0;
stage.on('mousemove', function() {
var ref1=135;//this is ref point for hair or curve no 1
var ref2=150;//hair no 2 and so on
var ref3=165;
var ref4=180;
var ref5=195;
var ref6=210;
var ref7=225;
var ref8=240;
var ref9=255;
var pos = stage.getMousePosition();
console.log("x="+pos.x+"&&"+"y="+pos.y)
if(between(pos.x,115,270) && between(pos.y,205,236))
{
if(pos.x>=ref1 && pos.x<=145){bend1=(pos.x-ref1)*(2.2);}
if(pos.x<=ref1 && pos.x>=125){bend1=(pos.x-ref1)*(2.2);}
if(pos.x>=ref2 && pos.x<=160){bend2=(pos.x-ref2)*(2.2);}
if(pos.x<=ref2 && pos.x>=140){bend2=(pos.x-ref2)*(2.2);}
if(pos.x>=ref3 && pos.x<=175){bend3=(pos.x-ref3)*(2.2);}
if(pos.x<=ref3 && pos.x>=155){bend3=(pos.x-ref3)*(2.2);}
if(pos.x>=ref4 && pos.x<=190){bend4=(pos.x-ref4)*(2.2);}
if(pos.x<=ref4 && pos.x>=170){bend4=(pos.x-ref4)*(2.2);}
if(pos.x>=ref5 && pos.x<=205){bend5=(pos.x-ref5)*(2.2);}
if(pos.x<=ref5 && pos.x>=185){bend5=(pos.x-ref5)*(2.2);}
if(pos.x>=ref6 && pos.x<=220){bend6=(pos.x-ref6)*(2.2);}
if(pos.x<=ref6 && pos.x>=200){bend6=(pos.x-ref6)*(2.2);}
if(pos.x>=ref7 && pos.x<=235){bend7=(pos.x-ref7)*(2.2);}
if(pos.x<=ref7 && pos.x>=215){bend7=(pos.x-ref7)*(2.2);}
if(pos.x>=ref8 && pos.x<=250){bend8=(pos.x-ref8)*(2.2);}
if(pos.x<=ref8 && pos.x>=230){bend8=(pos.x-ref8)*(2.2);}
if(pos.x>=ref9 && pos.x<=265){bend9=(pos.x-ref9)*(2.2);}
if(pos.x<=ref9 && pos.x>=245){bend9=(pos.x-ref9)*(2.2);}
}
hd.push(new Hair(0, 0, 0, 0, 0, 0, 0+bend1, 0));
layer.add(hd[0]);
hd.push(new Hair(15, 0, 15, 0, 15, 0, 15+bend2, 0));
layer.add(hd[1]);
hd.push(new Hair(30, 0, 30, 0, 30, 0, 30+bend3, 0));
layer.add(hd[2]);
hd.push(new Hair(45, 0, 45, 0, 45, 0, 45+bend4, 0));
layer.add(hd[3]);
hd.push(new Hair(60, 0, 60, 0, 60, 0, 60+bend5, 0));
layer.add(hd[4]);
hd.push(new Hair(75, 0, 75, 0, 75, 0, 75+bend6, 0));
layer.add(hd[5]);
hd.push(new Hair(90, 0, 90, 0, 90, 0, 90+bend7, 0));
layer.add(hd[6]);
hd.push(new Hair(105, 0, 105, 0, 105, 0, 105+bend8, 0));
layer.add(hd[7]);
hd.push(new Hair(120, 0, 120, 0, 120, 0, 120+bend9, 0));
layer.add(hd[8]);
stage.add(layer);
console.log("bend1="+bend1);
});
function between(val, min, max) {
return val >= min && val <= max;
}
window.onload = function() {
init();
};