Skip to content

Instantly share code, notes, and snippets.

@zinkkrysty
Last active April 19, 2016 15:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zinkkrysty/015391e8a14fa3f3f210 to your computer and use it in GitHub Desktop.
Save zinkkrysty/015391e8a14fa3f3f210 to your computer and use it in GitHub Desktop.
Drawing tool made with Bonsai.js (http://orbit.bonsaijs.org/)
var myStage;
var startDrag;
var myRect;
//
//Shape abstraction
//
//var RectShape = function(x, y, width, height){
// this.bonsaiRect = new Rect(x, y, width, height);
// return this;
//};
//RectShape.prototype = {
// setWidth: function(){
//
// }
//};
myStage = bonsai.Path.rect(0, 0, 500, 500).attr({fillColor: 'white'});
stage.addChild(myStage);
//
// Build a grid
//
maxWidth = 500;
maxHeight = 500;
gridSpace = 20;
i = gridSpace;
// Vertical grid
while (i < maxWidth) {
new Path()
.moveTo(i,0)
.lineTo(i,maxHeight)
.stroke('#ccc', 1)
.addTo(stage);
i+= gridSpace;
}
// Horizontal grid
i = gridSpace;
while (i < maxHeight) {
new Path()
.moveTo(0,i)
.lineTo(maxWidth,i)
.stroke('#ccc', 1)
.addTo(stage);
i+= gridSpace;
}
var moveListener = function(e){
console.log(e.x, e.y);
matrix = myRect.getAbsoluteMatrix();
console.log("myRect width: " + myRect.getBoundingBox(matrix).width);
endWidth = e.x - startDrag.x;
endHeight = e.y - startDrag.y;
// Because the initial size is [1,1], the scale is the finalDimension/1 = 1
myRect.attr({
scaleX: endWidth,
scaleY: endHeight
});
};
stage.on('pointerdown', function(data){
console.log(data.x);
startDrag = {x: data.x, y: data.y};
myRect = bonsai.Path.rect(data.x, data.y, 1, 1).attr({fillColor: 'red'});
stage.addChild(myRect);
myRect.on('click', function(e){
myRect.x = e.x;
});
stage.addListener('pointermove', moveListener);
});
stage.on('pointerup', function(data){
stage.removeAllListeners('pointermove');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment