Tween transition in Kinetic js - transition

I've been using the old 'transitionTo()' in my program but since Kineticjs is using Tween, i am a bit lost.
I've tried a simple shape transition using Tween and i've got some issues:
If you drag the shape to another point before doing anything, then click on the button for the transition, the shape comes back to the original hard coded coordinates and then does the transition.
I want the shape to start the transition where it has been dropped.
2.The 1st time it will do the transition, but afterwards it won't take the whole duration. It will just shift to the end point of the transition, like mentioned here.
Some codes:
var rect = new Kinetic.Rect({
x: 100,
y: 100,
width: 100,
height: 50,
fill: 'green',
stroke: 'black',
strokeWidth: 2,
draggable: true
});
layer.add(rect);
stage.add(layer);
var tween = new Kinetic.Tween({
node: rect,
x: 200,
y: 200,
rotation: 0,
duration:5
});
jsFiddle provided above.
Any help will be appreciated; thanx :)

This is what I would propose to solve your problems :
var stage = new Kinetic.Stage({
container: 'container',
width: 700,
height: 300
});
var layer = new Kinetic.Layer();
var rect = new Kinetic.Rect({
x: 100,
y: 100,
width: 100,
height: 50,
fill: 'green',
stroke: 'black',
strokeWidth: 2,
draggable: true
});
layer.add(rect);
stage.add(layer);
document.getElementById('run').addEventListener('click', function() {
var tween = new Kinetic.Tween({
node: rect,
x: 200,
y: 200,
rotation: 0,
duration:5
});
tween.play();
}, false);
Just instantiate the Tween transition at the moment, you want to use it. Otherwise, the transition will start with the position at the moment you instantiated it.
Here is the fork of your fiddle with my proposal : http://jsfiddle.net/kMvzy/

Related

In SwiftUI, "onHover" does not call back when Path draws non-filled graphics

In SwiftUI, when I use Path to draw a non-filled graphic, for example, draw a hollow rectangle, as follows:
code show as below:
Path {
$0.addRect(CGRect(x: 100, y: 100, width: 200, height: 200))
}.stroke(Color.red,lineWidth: 10).onHover { isHover in
print(isHover)
}
when I set "onHover", when the mouse moves to the graph, the "onHover" method does not make a callback. Is there a good way to monitor the hover state of Path

How to add border radius for custom Rectangular (by clipRectByRect ) in echarts?

Im trying to draw a gantt figure by the function below, but seems like the official documentations and examples didn't give the way to add border radius to those kind of rect, is there anyone know how to deal with it ?
function renderItem(params, api) {
var categoryIndex = api.value(0);
var start = api.coord([api.value(1), categoryIndex]);
var end = api.coord([api.value(2), categoryIndex]);
var height = api.size([0, 1])[1] * 0.6;
var rectShape = echarts.graphic.clipRectByRect({
x: start[0],
y: start[1] - height / 2,
width: end[0] - start[0],
height: height
}, {
x: params.coordSys.x,
y: params.coordSys.y,
width: params.coordSys.width,
height: params.coordSys.height
});
return rectShape && {
type: 'rect',
transition: ['shape'],
shape: rectShape,
style: api.style()
};
They do provide an approach for custom shape to add border radius, it can be find in the documentation. it is shape{ r: 10}

Bitmap cache as image source

is it possible to create bitmap cache of shape and use it for beginBitmapFill?
now i have code:
var shape = new createjs.Shape();
shape.graphics.beginStroke('red').moveTo(0, 0).lineTo(150, 0).lineTo(150, 150).lineTo(0, 150).lineTo(0, 0).moveTo(0, 50).lineTo(150, 50).moveTo(0, 100).lineTo(150, 100).moveTo(50, 0).lineTo(50, 150).moveTo(100, 0).lineTo(100, 150);
shape.cache(0, 0, 150, 150);
//shape.graphics.clear();
//shape.graphics.beginBitmapFill(img); //, "repeat", matrix)
shape.graphics.beginBitmapFill(shape.cacheCanvas);
stage.addChild(shape);
i found solution:
var shape = new createjs.Shape();
shape.graphics.beginStroke('red').moveTo(0, 0).lineTo(150, 0).lineTo(150, 150).lineTo(0, 150).lineTo(0, 0).moveTo(0, 50).lineTo(150, 50).moveTo(0, 100).lineTo(150, 100).moveTo(50, 0).lineTo(50, 150).moveTo(100, 0).lineTo(100, 150);
shape.cache(0, 0, 150, 150);
shape.graphics.clear().beginBitmapFill(shape.cacheCanvas).drawRect(0, 0, 300, 300);
shape.cache(0, 0, 300, 300);
stage.addChild(shape);

PhysicsJS - moving an object

I'm trying to figure out how to use PhysicsJS. I first off just want to simply figure out how to change lets say a position or a speed of an object on click... but I just cant figure it out!
( function()
{
var viewWidth = 500,
viewHeight = 300,
renderer = Physics.renderer( 'canvas',
{
el: 'viewport',
width: viewWidth,
height: viewHeight,
meta: false,
styles:
{
'circle' :
{
strokeStyle: 'hsla(60, 37%, 17%, 1)',
lineWidth: 1,
fillStyle: 'hsla(60, 37%, 57%, 0.8)',
angleIndicator: 'hsla(60, 37%, 17%, 0.4)'
}
}
}),
viewportBounds = Physics.aabb(0, 0, viewWidth, viewHeight),
constraint = {
aabb: viewportBounds,
restitution: 0.99,
cof: 0.99
},
ballOptions = {
x: 100, // x-coordinate
y: 100, // y-coordinate
vx: 0.0, // velocity in x-direction
vy: 0.0, // velocity in y-direction
radius: 20
},
gravity = Physics.behavior('constant-acceleration',
{
acc: { x : 0, y: 0.0004 }
}),
ball = Physics.body('circle', ballOptions );
Physics( function( world )
{
// add the renderer
world.add( renderer );
// add circle
world.add( ball );
// subscribe to ticker to advance the simulation
Physics.util.ticker.subscribe(function( time, dt )
{
world.step( time );
});
// on every step...
world.subscribe( 'step', function()
{
world.render();
});
world.subscribe( 'collisions:detected', function( $collision )
{
});
var onElementClick = function()
{
// do something
};
document.getElementById( 'viewport' ).addEventListener( 'click', onElementClick, false );
// Lets GO!
Physics.util.ticker.start();
});
})();
any help much appreciated
One option is to take the gravity that was created but never added to the world and do that onclick.
world.add(gravity);
That's cheating in the sense that you asked about changing the position or speed of an object. To do that, modify the state of the ball. See the docs on Bodies, specifically the properties. You can set state.pos to move it. To put it in motion, set the velocity:
ball.state.vel.set(.1,-.5); // move right and upward
jsfiddle that sets velocity

gamequery collision detection doesnt react

i created 2 sprite objects and want to check if they collide, but it doesnt seem to work.
My code:
creation of the sprites:**
.addGroup("flying", {width: 366, height: 254})
.addSprite("flyinganimation",{animation: flight,
posx: 0, posy: 0, width: 366, height: 254})
.addGroup("obstacles", {width: 169, height: 67})
.addSprite("obstaclenames", {animation: obstacleimage,
width: 169,height: 67,
posx: 300,
posy: 400})
looped code in the callback function:**
var collision = $("#flying,.group").collision("#obstacles,.group");
if (collision > 0)
{
document.write ("collision");
}
I see two potential problems here:
1) collision() returns a jQuery object so you will need to do a .size() in order to retrieve the number of collisions
2) you should apply collision() to only one object.
To sum up your first line should look like this:
var collision = $("#flying").collision("#obstacles,.group").size();