konva/src/Stage.js

599 lines
20 KiB
JavaScript
Raw Normal View History

(function() {
// CONSTANTS
2013-03-24 14:50:51 +08:00
var STAGE = 'Stage',
STRING = 'string',
PX = 'px',
MOUSEOUT = 'mouseout',
MOUSELEAVE = 'mouseleave',
MOUSEOVER = 'mouseover',
MOUSEENTER = 'mouseenter',
MOUSEMOVE = 'mousemove',
MOUSEDOWN = 'mousedown',
MOUSEUP = 'mouseup',
CLICK = 'click',
DBL_CLICK = 'dblclick',
2013-06-02 13:03:02 +08:00
TOUCHSTART = 'touchstart',
TOUCHEND = 'touchend',
2013-03-24 14:50:51 +08:00
TAP = 'tap',
DBL_TAP = 'dbltap',
TOUCHMOVE = 'touchmove',
DIV = 'div',
RELATIVE = 'relative',
INLINE_BLOCK = 'inline-block',
KINETICJS_CONTENT = 'kineticjs-content',
SPACE = ' ',
UNDERSCORE = '_',
2013-03-24 14:50:51 +08:00
CONTAINER = 'container',
EMPTY_STRING = '',
EVENTS = [MOUSEDOWN, MOUSEMOVE, MOUSEUP, MOUSEOUT, TOUCHSTART, TOUCHMOVE, TOUCHEND, MOUSEOVER],
// cached variables
eventsLength = EVENTS.length;
2013-04-08 16:02:08 +08:00
function addEvent(ctx, eventName) {
ctx.content.addEventListener(eventName, function(evt) {
ctx[UNDERSCORE + eventName](evt);
2013-04-08 16:02:08 +08:00
}, false);
}
Kinetic.Util.addMethods(Kinetic.Stage, {
___init: function(config) {
// call super constructor
Kinetic.Container.call(this, config);
2013-03-24 14:50:51 +08:00
this.nodeType = STAGE;
this._id = Kinetic.Global.idCounter++;
this._buildDOM();
this._bindContentEvents();
Kinetic.Global.stages.push(this);
},
_validateAdd: function(child) {
if (child.getType() !== 'Layer') {
Kinetic.Util.error('You may only add layers to the stage.');
}
},
2012-12-17 12:52:07 +08:00
/**
* set container dom element which contains the stage wrapper div element
* @method
* @memberof Kinetic.Stage.prototype
2012-12-17 12:52:07 +08:00
* @param {DomElement} container can pass in a dom element or id string
*/
setContainer: function(container) {
2013-03-24 14:50:51 +08:00
if( typeof container === STRING) {
container = document.getElementById(container);
}
this._setAttr(CONTAINER, container);
return this;
},
draw: function() {
Kinetic.Node.prototype.draw.call(this);
return this;
},
/**
2012-12-17 12:52:07 +08:00
* draw layer scene graphs
* @name draw
* @method
* @memberof Kinetic.Stage.prototype
*/
/**
2012-12-17 12:52:07 +08:00
* draw layer hit graphs
* @name drawHit
* @method
* @memberof Kinetic.Stage.prototype
*/
/**
* set height
* @method
* @memberof Kinetic.Stage.prototype
* @param {Number} height
*/
setHeight: function(height) {
Kinetic.Node.prototype.setHeight.call(this, height);
this._resizeDOM();
return this;
},
/**
* set width
* @method
* @memberof Kinetic.Stage.prototype
* @param {Number} width
*/
setWidth: function(width) {
Kinetic.Node.prototype.setWidth.call(this, width);
this._resizeDOM();
return this;
},
/**
* clear all layers
* @method
* @memberof Kinetic.Stage.prototype
*/
clear: function() {
2013-03-24 14:50:51 +08:00
var layers = this.children,
2013-04-04 03:43:32 +08:00
len = layers.length,
2013-03-24 14:50:51 +08:00
n;
2013-03-24 14:50:51 +08:00
for(n = 0; n < len; n++) {
layers[n].clear();
}
return this;
},
/**
* remove stage
* @method
* @memberof Kinetic.Stage.prototype
*/
destroy: function() {
var content = this.content;
Kinetic.Container.prototype.destroy.call(this);
if(content && Kinetic.Util._isInDocument(content)) {
2013-03-24 14:50:51 +08:00
this.getContainer().removeChild(content);
}
},
/**
* get mouse position for desktop apps
* @method
* @memberof Kinetic.Stage.prototype
*/
2012-12-17 12:52:07 +08:00
getMousePosition: function() {
return this.mousePos;
},
/**
* get touch position for mobile apps
* @method
* @memberof Kinetic.Stage.prototype
*/
2012-12-17 12:52:07 +08:00
getTouchPosition: function() {
return this.touchPos;
},
/**
* get pointer position which can be a touc position or mouse position
* @method
* @memberof Kinetic.Stage.prototype
*/
getPointerPosition: function() {
return this.getTouchPosition() || this.getMousePosition();
},
getStage: function() {
return this;
},
/**
* get stage content div element which has the
* the class name "kineticjs-content"
* @method
* @memberof Kinetic.Stage.prototype
*/
getContent: function() {
return this.content;
},
/**
2012-12-17 12:52:07 +08:00
* Creates a composite data URL and requires a callback because the composite is generated asynchronously.
* @method
* @memberof Kinetic.Stage.prototype
* @param {Object} config
2012-12-17 12:52:07 +08:00
* @param {Function} config.callback function executed when the composite has completed
* @param {String} [config.mimeType] can be "image/png" or "image/jpeg".
* "image/png" is the default
2012-12-17 12:52:07 +08:00
* @param {Number} [config.x] x position of canvas section
* @param {Number} [config.y] y position of canvas section
* @param {Number} [config.width] width of canvas section
* @param {Number} [config.height] height of canvas section
* @param {Number} [config.quality] jpeg quality. If using an "image/jpeg" mimeType,
* you can specify the quality from 0 to 1, where 0 is very poor quality and 1
* is very high quality
*/
toDataURL: function(config) {
2013-06-02 13:03:02 +08:00
config = config || {};
var mimeType = config.mimeType || null,
quality = config.quality || null,
x = config.x || 0,
y = config.y || 0,
canvas = new Kinetic.SceneCanvas({
width: config.width || this.getWidth(),
height: config.height || this.getHeight(),
pixelRatio: 1
}),
context = canvas.getContext(),
layers = this.children;
if(x || y) {
context.translate(-1 * x, -1 * y);
}
function drawLayer(n) {
2013-03-24 14:50:51 +08:00
var layer = layers[n],
layerUrl = layer.toDataURL(),
imageObj = new Image();
imageObj.onload = function() {
context.drawImage(imageObj, 0, 0);
if(n < layers.length - 1) {
drawLayer(n + 1);
}
else {
config.callback(canvas.toDataURL(mimeType, quality));
}
};
imageObj.src = layerUrl;
}
drawLayer(0);
},
/**
* converts stage into an image.
* @method
* @memberof Kinetic.Stage.prototype
* @param {Object} config
2012-12-17 12:52:07 +08:00
* @param {Function} config.callback function executed when the composite has completed
* @param {String} [config.mimeType] can be "image/png" or "image/jpeg".
* "image/png" is the default
2012-12-17 12:52:07 +08:00
* @param {Number} [config.x] x position of canvas section
* @param {Number} [config.y] y position of canvas section
* @param {Number} [config.width] width of canvas section
* @param {Number} [config.height] height of canvas section
* @param {Number} [config.quality] jpeg quality. If using an "image/jpeg" mimeType,
* you can specify the quality from 0 to 1, where 0 is very poor quality and 1
* is very high quality
*/
toImage: function(config) {
var cb = config.callback;
config.callback = function(dataUrl) {
Kinetic.Util._getImage(dataUrl, function(img) {
cb(img);
});
};
this.toDataURL(config);
},
/**
* get visible intersection object that contains shape and pixel data. This is the preferred
* method for determining if a point intersects a shape or not
* @method
* @memberof Kinetic.Stage.prototype
* @param {Object} pos point object
*/
getIntersection: function() {
var pos = Kinetic.Util._getXY(Array.prototype.slice.call(arguments)),
layers = this.getChildren(),
2013-03-24 14:50:51 +08:00
len = layers.length,
end = len - 1,
n, obj;
2013-03-24 14:50:51 +08:00
for(n = end; n >= 0; n--) {
obj = layers[n].getIntersection(pos);
if (obj) {
return obj;
}
}
return null;
},
_resizeDOM: function() {
if(this.content) {
2013-03-24 14:50:51 +08:00
var width = this.getWidth(),
height = this.getHeight(),
layers = this.getChildren(),
len = layers.length,
n, layer;
// set content dimensions
2013-03-24 14:50:51 +08:00
this.content.style.width = width + PX;
this.content.style.height = height + PX;
this.bufferCanvas.setSize(width, height, 1);
this.hitCanvas.setSize(width, height);
// set pointer defined layer dimensions
2013-03-24 14:50:51 +08:00
for(n = 0; n < len; n++) {
layer = layers[n];
layer.getCanvas().setSize(width, height);
layer.hitCanvas.setSize(width, height);
layer.draw();
}
}
},
/**
* add layer to stage
* @method
* @memberof Kinetic.Stage.prototype
2012-12-17 12:52:07 +08:00
* @param {Kinetic.Layer} layer
*/
add: function(layer) {
Kinetic.Container.prototype.add.call(this, layer);
layer.canvas.setSize(this.attrs.width, this.attrs.height);
layer.hitCanvas.setSize(this.attrs.width, this.attrs.height);
// draw layer and append canvas to container
layer.draw();
this.content.appendChild(layer.canvas.element);
// chainable
return this;
},
getParent: function() {
return null;
},
getLayer: function() {
return null;
},
/**
* returns a {@link Kinetic.Collection} of layers
* @method
* @memberof Kinetic.Stage.prototype
*/
getLayers: function() {
return this.getChildren();
},
_setPointerPosition: function(evt) {
if(!evt) {
evt = window.event;
}
this._setMousePosition(evt);
this._setTouchPosition(evt);
},
_bindContentEvents: function() {
var that = this,
2013-04-08 16:02:08 +08:00
n;
2013-03-24 14:50:51 +08:00
for (n = 0; n < eventsLength; n++) {
2013-04-08 16:02:08 +08:00
addEvent(this, EVENTS[n]);
}
},
_mouseover: function(evt) {
this._fire(MOUSEOVER, evt);
},
_mouseout: function(evt) {
this._setPointerPosition(evt);
2013-03-24 14:50:51 +08:00
var go = Kinetic.Global,
targetShape = this.targetShape;
if(targetShape && !go.isDragging()) {
targetShape._fireAndBubble(MOUSEOUT, evt);
targetShape._fireAndBubble(MOUSELEAVE, evt);
this.targetShape = null;
}
this.mousePos = undefined;
this._fire(MOUSEOUT, evt);
},
_mousemove: function(evt) {
this._setPointerPosition(evt);
var go = Kinetic.Global,
dd = Kinetic.DD,
2013-03-24 14:50:51 +08:00
obj = this.getIntersection(this.getPointerPosition()),
shape;
if(obj) {
2013-03-24 14:50:51 +08:00
shape = obj.shape;
if(shape) {
if(!go.isDragging() && obj.pixel[3] === 255 && (!this.targetShape || this.targetShape._id !== shape._id)) {
2013-07-28 16:01:39 +08:00
shape._fireAndBubble(MOUSEOVER, evt, this.targetShape);
shape._fireAndBubble(MOUSEENTER, evt, this.targetShape);
if(this.targetShape) {
this.targetShape._fireAndBubble(MOUSEOUT, evt, shape);
this.targetShape._fireAndBubble(MOUSELEAVE, evt, shape);
}
this.targetShape = shape;
}
else {
shape._fireAndBubble(MOUSEMOVE, evt);
}
}
}
/*
* if no shape was detected, clear target shape and try
* to run mouseout from previous target shape
*/
else {
2013-07-28 16:01:39 +08:00
this._fire(MOUSEMOVE, evt);
if(this.targetShape && !go.isDragging()) {
this.targetShape._fireAndBubble(MOUSEOUT, evt);
this.targetShape._fireAndBubble(MOUSELEAVE, evt);
this.targetShape = null;
}
}
if(dd) {
dd._drag(evt);
}
// always call preventDefault for desktop events because some browsers
// try to drag and drop the canvas element
2013-07-24 02:15:04 +08:00
if (evt.preventDefault) {
evt.preventDefault();
}
},
_mousedown: function(evt) {
this._setPointerPosition(evt);
2013-06-02 13:03:02 +08:00
var go = Kinetic.Global,
obj = this.getIntersection(this.getPointerPosition()),
shape = obj && obj.shape ? obj.shape : this;
go.listenClickTap = true;
this.clickStartShape = shape;
shape._fireAndBubble(MOUSEDOWN, evt);
// always call preventDefault for desktop events because some browsers
// try to drag and drop the canvas element
2013-07-24 02:15:04 +08:00
if (evt.preventDefault) {
evt.preventDefault();
}
},
_mouseup: function(evt) {
this._setPointerPosition(evt);
var that = this,
go = Kinetic.Global,
2013-03-24 14:50:51 +08:00
obj = this.getIntersection(this.getPointerPosition()),
shape = obj && obj.shape ? obj.shape : this;
shape._fireAndBubble(MOUSEUP, evt);
// detect if click or double click occurred
if(go.listenClickTap && shape._id === this.clickStartShape._id) {
shape._fireAndBubble(CLICK, evt);
if(go.inDblClickWindow) {
shape._fireAndBubble(DBL_CLICK, evt);
go.inDblClickWindow = false;
}
else {
go.inDblClickWindow = true;
}
setTimeout(function() {
go.inDblClickWindow = false;
}, go.dblClickWindow);
}
go.listenClickTap = false;
// always call preventDefault for desktop events because some browsers
// try to drag and drop the canvas element
2013-07-24 02:15:04 +08:00
if (evt.preventDefault) {
evt.preventDefault();
}
},
_touchstart: function(evt) {
2013-06-02 13:03:02 +08:00
this._setPointerPosition(evt);
var go = Kinetic.Global,
obj = this.getIntersection(this.getPointerPosition()),
shape = obj && obj.shape ? obj.shape : this;
go.listenClickTap = true;
this.tapStartShape = shape;
shape._fireAndBubble(TOUCHSTART, evt);
// only call preventDefault if the shape is listening for events
2013-07-24 02:15:04 +08:00
if (shape.isListening() && evt.preventDefault) {
evt.preventDefault();
}
},
_touchend: function(evt) {
this._setPointerPosition(evt);
var that = this,
go = Kinetic.Global,
2013-03-24 14:50:51 +08:00
obj = this.getIntersection(this.getPointerPosition()),
shape = obj && obj.shape ? obj.shape : this;
shape._fireAndBubble(TOUCHEND, evt);
// detect if tap or double tap occurred
if(go.listenClickTap && shape._id === this.tapStartShape._id) {
shape._fireAndBubble(TAP, evt);
if(go.inDblClickWindow) {
shape._fireAndBubble(DBL_TAP, evt);
go.inDblClickWindow = false;
}
else {
go.inDblClickWindow = true;
}
setTimeout(function() {
go.inDblClickWindow = false;
}, go.dblClickWindow);
}
go.listenClickTap = false;
// only call preventDefault if the shape is listening for events
2013-07-24 02:15:04 +08:00
if (shape.isListening() && evt.preventDefault) {
evt.preventDefault();
}
},
_touchmove: function(evt) {
this._setPointerPosition(evt);
2013-03-24 14:50:51 +08:00
var dd = Kinetic.DD,
obj = this.getIntersection(this.getPointerPosition()),
shape = obj && obj.shape ? obj.shape : this;
shape._fireAndBubble(TOUCHMOVE, evt);
// start drag and drop
if(dd) {
dd._drag(evt);
}
// only call preventDefault if the shape is listening for events
2013-07-24 02:15:04 +08:00
if (shape.isListening() && evt.preventDefault) {
evt.preventDefault();
}
},
_setMousePosition: function(evt) {
2013-03-24 14:50:51 +08:00
var mouseX = evt.clientX - this._getContentPosition().left,
mouseY = evt.clientY - this._getContentPosition().top;
this.mousePos = {
x: mouseX,
y: mouseY
};
},
_setTouchPosition: function(evt) {
2013-03-24 14:50:51 +08:00
var touch, touchX, touchY;
if(evt.touches !== undefined && evt.touches.length === 1) {
// one finger
2013-03-24 14:50:51 +08:00
touch = evt.touches[0];
2013-03-24 14:50:51 +08:00
// get the information for finger #1
touchX = touch.clientX - this._getContentPosition().left;
touchY = touch.clientY - this._getContentPosition().top;
this.touchPos = {
x: touchX,
y: touchY
};
}
},
_getContentPosition: function() {
var rect = this.content.getBoundingClientRect();
return {
top: rect.top,
left: rect.left
};
},
_buildDOM: function() {
var container = this.getContainer();
// clear content inside container
container.innerHTML = EMPTY_STRING;
// content
2013-03-24 14:50:51 +08:00
this.content = document.createElement(DIV);
this.content.style.position = RELATIVE;
this.content.style.display = INLINE_BLOCK;
this.content.className = KINETICJS_CONTENT;
container.appendChild(this.content);
this.bufferCanvas = new Kinetic.SceneCanvas();
this.hitCanvas = new Kinetic.HitCanvas();
this._resizeDOM();
},
_onContent: function(typesStr, handler) {
2013-03-24 14:50:51 +08:00
var types = typesStr.split(SPACE),
len = types.length,
n, baseEvent;
2013-03-24 14:50:51 +08:00
for(n = 0; n < len; n++) {
baseEvent = types[n];
this.content.addEventListener(baseEvent, handler, false);
}
}
});
Kinetic.Util.extend(Kinetic.Stage, Kinetic.Container);
// add getters and setters
Kinetic.Node.addGetter(Kinetic.Stage, 'container');
/**
* get container DOM element
* @name getContainer
* @method
* @memberof Kinetic.Stage.prototype
*/
})();