简体   繁体   English

补间Kineticjs形状

[英]Tweening a Kineticjs Shape

I have a custom shape created in Kineticjs and I need the shape to tween like a line. 我在Kineticjs中创建了一个自定义形状,我需要像直线一样补间形状。 How would I go about doing this? 我将如何去做呢? I've tried creating a Kinetic Tween but that doesn't seem to work correctly. 我曾尝试创建Kinetic Tween,但似乎无法正常工作。

var grnStripe = new Image();
grnStripe.onload = start;
grnStripe.src = 'images/GreenStripe-01.png';
function start(){
grnLine = new Kinetic.Shape({
    sceneFunc: function (context) {
        var ctx = this.getContext()._context;
        var pattern = context.createPattern(grnStripe, 'repeat');
        ctx.beginPath();
        ctx.moveTo(379.5, 270);
        ctx.lineTo(379.5, 270);
        ctx.strokeStyle = pattern;
        ctx.lineWidth = 2.5;
        ctx.stroke();
    }
});
line_layer.add(grnLine);
line_layer.draw();
}

I want the shape(line) to tween to a height of 310. Again, I've tried to create a Kinetic Tween but it's not seeming to work. 我希望shape(line)补间到310的高度。再次,我尝试创建Kinetic Tween,但似乎不起作用。

我需要的模拟

Yes, you can Kinetic.Tween a Kinetic.Shape--even a shape with a pattern stroke. 是的,您可以Kinetic.Tween Kinetic.Shape-甚至具有图案笔触的形状。

[ A general answer about tweening a Kinetic.Shape ] [关于补间Kinetic.Shape的一般答案]

The key to tweening a Kinetic.Shape is having sceneFunc change using a Kinetic.Shape property. 关键要补间一个Kinetic.Shape是有sceneFunc使用Kinetic.Shape属性更改。

For example, this sceneFunc will draw a rectange based on that Kinetic.Shape's x & y properties. 例如,此sceneFunc将基于Kinetic.Shape的x&y属性绘制一个矩形。

sceneFunc: function(context) {
  var x=this.x();
  var y=this.y();
  context.fillRect(x,y,20,20);
},

Then you can use a Kinetic.Tween to change the Kinetic.Shapes' x/y which in turn will animate the rect. 然后,您可以使用Kinetic.Tween更改Kinetic.Shapes的x / y,从而使rect成为动画。

For example this tween will animate a Kinetic.Shape called myShape from its current x/y to and x/y of [100,100]. 例如,此补间动画从当前的x / y到x / y为[100,100]的Kinetic.Shape动画为myShape。

tween = new Kinetic.Tween({
    node: myShape, 
    duration: 1,
    x: 100,
    y: 100,
  });

[ A specific answer based on one of your previous questions about pattern-stroked lines ] [基于您先前有关图案描边线的问题之一的特定答案]

The Kinetic.Shape context does not allow you to draw a pattern-stroked line. Kinetic.Shape上下文不允许您绘制图案描边的线。

As a workaround you can fetch the actual html context which does allow pattern-stroked lines. 解决方法是,您可以获取实际的html上下文,该上下文确实允许使用有图案线条的行。

A "ghosting problem" when tweening: 补间时出现“重影问题”:

However, when you want to tween this pattern-filled line you have a "ghosting" problem because Kinetic actually uses 2 canvases for every layer and your pattern-filled line is incorrectly positioned on that second hit-canvas (a "ghost"). 但是,当您要补间这条填充图案的行时,您会遇到“重影”问题,因为Kinetic实际上为每个图层使用了2个画布,并且您的填充图案的线错误地位于了第二个点击画布(“重影”)上。 This is to be expected because Kinetic cannot be expected to know what you've drawn on your actual html context. 这是可以预期的,因为不能期望Kinetic知道您在实际html上下文中所绘制的内容。

You can fix this ghosting problem by defining hitFunc in your custom Kinetic.Shape. 您可以通过在自定义Kinetic.Shape中定义hitFunc来解决此重影问题。 That allows Kinetic to correctly position your pattern-filled line on its second hit-canvas. 这使Kinetic可以将图案填充线正确定位在第二条击中画布上。 The ghosting is gone. 重影消失了。

Here is a Fiddle demonstrating the workaround for your pattern-filled line: 这是一个小提琴,展示了您的填充模式行的解决方法:

http://jsfiddle.net/m1erickson/GpSbd/ http://jsfiddle.net/m1erickson/GpSbd/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM