MOVE TO BOTTOM,UP,setZIndex in kinetic.js

<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
#buttons {
position: absolute;
left: 10px;
top: 0px;
}
button {
margin-top: 10px;
display: block;
}
</style>
</head>
<body>
<div id=”container”></div>
<div id=”buttons”>
<button id=”toTop”>
Move yellow box to top
</button>
<button id=”toBottom”>
Move yellow box to bottom
</button>
<button id=”up”>
Move yellow box up
</button>
<button id=”down”>
Move yellow box down
</button>
<button id=”zIndex”>
Set yellow box zIndex to 3
</button>
</div>
<script src=”http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v5.0.0.min.js”></script&gt;
<script defer=”defer”>
var stage = new Kinetic.Stage({
container: ‘container’,
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
var offsetX = 0;
var offsetY = 0;
var colors = [‘red’, ‘orange’, ‘yellow’, ‘green’, ‘blue’, ‘purple’];
var yellowBox = null;

for(var n = 0; n < 6; n++) {
// anonymous function to induce scope
(function() {
var i = n;
var box = new Kinetic.Rect({
x: i * 30 + 210,
y: i * 18 + 40,
width: 100,
height: 50,
fill: colors[i],
stroke: ‘black’,
strokeWidth: 4,
draggable: true,
name: colors[i]
});

box.on(‘mouseover’, function() {
document.body.style.cursor = ‘pointer’;
});
box.on(‘mouseout’, function() {
document.body.style.cursor = ‘default’;
});
if(colors[i] === ‘yellow’) {
yellowBox = box;
}

layer.add(box);
})();
}

stage.add(layer);

// add button event bindings
document.getElementById(‘toTop’).addEventListener(‘click’, function() {
yellowBox.moveToTop();
layer.draw();
}, false);

document.getElementById(‘toBottom’).addEventListener(‘click’, function() {
yellowBox.moveToBottom();
layer.draw();
}, false);

document.getElementById(‘up’).addEventListener(‘click’, function() {
yellowBox.moveUp();
layer.draw();
}, false);

document.getElementById(‘down’).addEventListener(‘click’, function() {
yellowBox.moveDown();
layer.draw();
}, false);

document.getElementById(‘zIndex’).addEventListener(‘click’, function() {
yellowBox.setZIndex(3);
layer.draw();
}, false);
</script>
</body>
</html>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s