Tags: %TAGME{ tpaction="" web="SCMP118" tag="" }% view all tags

# Konva Group Activity

To be done in class as a group. Each student turns in result by December 16.

Moodle ### Goal

Learn how to use an object oriented graphic library in javascript.

###### Step 1 - Getting Started

We can use jsfiddle, as before, to do our coding. Here is a link to a jsfiddle that includes Konva: https://jsfiddle.net/jimskon/ahm4xc50/ ```var stage = new Konva.Stage({
container: 'container',   // id of container <div>
width: 500,
height: 500
});

// then create layer
var layer = new Konva.Layer();

// create our shape
var circle = new Konva.Circle({
x: stage.getWidth() / 2,
y: stage.getHeight() / 2,
fill: 'red',
stroke: 'black',
strokeWidth: 4,
draggable: true
});

// add the shape to the layer
###### Step 2 - add a square

```var square = new Konva.RegularPolygon({
x: (stage.getWidth() / 4)*3,
y: stage.getHeight() / 2,
sides: 4,
fill: 'blue',
stroke: 'black',
strokeWidth: 4
}); Can we rotate the square?

`square.rotate(45);`
###### Step 2 - spin square

Can we animate the square by making it spin?

We can use the Konva animation object. This runs and updates the screen quickly (I get around 60 frames a second). What does this mean? It means that the code must compute how much to change the objects on the screen based on how much time passes between every frame.

So we need know how much time has passed since the frame, and thus how much change to make. We can access the time since the last frame with frame.timeDiff, the time in milliseconds since the last frame. This number changes with system load. But if we make changes based on the time, the screen changes will be constant despite the variation in frame rate (this is true for all animation systems). You can allow access frame.frameRate, the current number of frames per second.

If we multiply the timeDiff/1000 times the amount to rotate per second, we get the amount to rotate for the last frame time.

Consider the following code:

```square.angle = 0;
square.rps = 0.05; // revolutions per second
```

Now lets make a function to update the square:

```function updateSquare(frame) {
// Animate the square
square.angle = frame.timeDiff / 1000 * square.rps * 360;
square.rotate(square.angle);
}

var anim = new Konva.Animation(function(frame) {
}, layer);

anim.start();
```
###### Step 3 - bounce circle

Let's make the ball bounce. First let's reduce the ball size to 40. Let's make a variable circleRadius, and change our circle to use this variable for the radius..

Now we want to move the ball at a certain speed. Let's say 200 pixels per second. the variable frame.frameRate gives us the number of frames per second. 1/frame.frameRate gives us the the fraction of a second between each frame. What we want to do is update the Y coordinate by 1/frame.frameRate * dy, where dy is the distance to move per second (200):

We will create a new funcrion updateCircle.

```var dy=200;

function updateCircle(frame) {
// animate the circles
// animate the circle
var newY=circle.y()+(1 / frame.frameRate) * dy;
circle.setY(newY);
}
```

Then call updateCircle from the animate function.

Of course it doesn't bounce, how do we do that? Use stage.getHeight() to get the height of the screen. Then reverse the direction when we hit the bottom.

```if ((newY + circleRadius) > stage.getHeight() ||
(newY - circleRadius) < 0) {
dy = dy * -1;
} else {
circle.setY(newY);
}```

We don't update the position on a bounce. Why?

Note that we use || for or in javascript. WE use && for and! Also note that you can still drag it!

###### Step 4 - bounce circle like a ball!

To this we need to simulate gravity and the dampening effect from each bounce. First we need to model the acceleration of gravity when falling, and the deceleration caused by gravity on rising. Consider:

`ay = 9.8 m/s`
2

This gives the amount of acceleration that occurs each second. Of course we don't move in meters per second, we move in pixels per second. What this means is the thing falling increases it speed by 9.8 m/s for each second pasts. So, in our case we start a 0 velocity. For each second that something falls, the speed increases by 9.8 m/s more. What I have found to be a good amount is to increase the speed by 20 each second. Thus we should see the following (in pixels per second), if starting from y=50 on the screen:

 Time Accel Velocity Position 0s 20 p/s2 0 p/s 50 1s 20 p/s2 20 p/s 50 2s 20 p/s2 40 p/s 70 3s 20 p/s2 60 p/s 110 4s 20 p/s2 80 p/s 170 5s 20 p/s2 100 p/s 250
Let's look at some code to do this!
```var circle = new Konva.Circle({

x: 50,
y: 50,
fill: "green",
stroke: 'black',
strokeWidth: 4,
draggable: true
});
circle.dy = 0; // current speed of the ball in pixels per second
circle.accel = 20; // acceleration in pixels/s^2
circle.damp = 0.90; // velocity to retain after bounce
```

Now let's look at some code to bounce the ball...

```function updateCircle(frame) {
// animate the circles

var newY = circle.y() + frame.timeDiff / 1000 * circle.dy;
// Check if we hit the  floor
if ((newY + circle.radius()) > stage.getHeight()) {
circle.dy = circle.dy * -1;
circle.dy = circle.dy * circle.damp;
// Check if we hit the ceiling
} else if (newY + circle.radius() < 0) {
circle.dy = circle.dy * -1;
} else {
circle.setY(newY);
}
// compute the newx speed based on the effect of gravity
circle.dy = circle.dy + circle.accel;

}
```
Working Bounching circle: https://jsfiddle.net/jimskon/ahm4xc50/127/ ###### Step 5 - Can we do more?
• Add side to side movement?    