konva/src/Stage.js

779 lines
27 KiB
JavaScript
Raw Normal View History

2014-02-27 08:49:18 +08:00
/*jshint unused:false */
(function() {
// CONSTANTS
2013-03-24 14:50:51 +08:00
var STAGE = 'Stage',
STRING = 'string',
PX = 'px',
2013-03-24 14:50:51 +08:00
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',
2014-05-01 21:11:03 +08:00
DOMMOUSESCROLL = 'DOMMouseScroll',
MOUSEWHEEL = 'mousewheel',
WHEEL = 'wheel',
CONTENT_MOUSEOUT = 'contentMouseout',
CONTENT_MOUSEOVER = 'contentMouseover',
CONTENT_MOUSEMOVE = 'contentMousemove',
CONTENT_MOUSEDOWN = 'contentMousedown',
CONTENT_MOUSEUP = 'contentMouseup',
CONTENT_CLICK = 'contentClick',
CONTENT_DBL_CLICK = 'contentDblclick',
CONTENT_TOUCHSTART = 'contentTouchstart',
CONTENT_TOUCHEND = 'contentTouchend',
CONTENT_DBL_TAP = 'contentDbltap',
CONTENT_TOUCHMOVE = 'contentTouchmove',
2013-03-24 14:50:51 +08:00
DIV = 'div',
RELATIVE = 'relative',
KONVA_CONTENT = 'konvajs-content',
2013-03-24 14:50:51 +08:00
SPACE = ' ',
UNDERSCORE = '_',
2013-03-24 14:50:51 +08:00
CONTAINER = 'container',
EMPTY_STRING = '',
2014-05-01 21:11:03 +08:00
EVENTS = [MOUSEDOWN, MOUSEMOVE, MOUSEUP, MOUSEOUT, TOUCHSTART, TOUCHMOVE, TOUCHEND, MOUSEOVER, DOMMOUSESCROLL, MOUSEWHEEL, WHEEL],
2013-11-28 13:34:06 +08:00
// cached variables
eventsLength = EVENTS.length;
2013-04-08 16:02:08 +08:00
function addEvent(ctx, eventName) {
2014-02-27 08:49:18 +08:00
ctx.content.addEventListener(eventName, function(evt) {
ctx[UNDERSCORE + eventName](evt);
}, false);
2013-04-08 16:02:08 +08:00
}
2015-04-08 23:26:43 +08:00
/**
* Stage constructor. A stage is used to contain multiple layers
* @constructor
* @memberof Konva
* @augments Konva.Container
* @param {Object} config
* @param {String|Element} config.container Container id or DOM element
* @@nodeParams
* @example
* var stage = new Konva.Stage({
* width: 500,
* height: 800,
* container: 'containerId'
* });
*/
Konva.Stage = function(config) {
this.___init(config);
};
2015-01-27 15:07:51 +08:00
Konva.Util.addMethods(Konva.Stage, {
___init: function(config) {
this.nodeType = STAGE;
// call super constructor
2015-01-27 15:07:51 +08:00
Konva.Container.call(this, config);
this._id = Konva.idCounter++;
this._buildDOM();
this._bindContentEvents();
this._enableNestedTransforms = false;
2015-01-27 15:07:51 +08:00
Konva.stages.push(this);
},
_validateAdd: function(child) {
if (child.getType() !== 'Layer') {
2015-01-27 15:07:51 +08:00
Konva.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
2015-01-27 15:07:51 +08:00
* @memberof Konva.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) {
var id = container;
2015-01-27 15:07:51 +08:00
container = Konva.document.getElementById(container);
if (!container) {
throw 'Can not find container in document with id ' + id;
}
}
this._setAttr(CONTAINER, container);
return this;
},
2013-12-10 14:31:18 +08:00
shouldDrawHit: function() {
return true;
},
draw: function() {
2015-01-27 15:07:51 +08:00
Konva.Node.prototype.draw.call(this);
return this;
},
/**
2012-12-17 12:52:07 +08:00
* draw layer scene graphs
* @name draw
* @method
2015-01-27 15:07:51 +08:00
* @memberof Konva.Stage.prototype
*/
/**
2012-12-17 12:52:07 +08:00
* draw layer hit graphs
* @name drawHit
* @method
2015-01-27 15:07:51 +08:00
* @memberof Konva.Stage.prototype
*/
/**
* set height
* @method
2015-01-27 15:07:51 +08:00
* @memberof Konva.Stage.prototype
* @param {Number} height
*/
setHeight: function(height) {
2015-01-27 15:07:51 +08:00
Konva.Node.prototype.setHeight.call(this, height);
this._resizeDOM();
return this;
},
/**
* set width
* @method
2015-01-27 15:07:51 +08:00
* @memberof Konva.Stage.prototype
* @param {Number} width
*/
setWidth: function(width) {
2015-01-27 15:07:51 +08:00
Konva.Node.prototype.setWidth.call(this, width);
this._resizeDOM();
return this;
},
/**
* clear all layers
* @method
2015-01-27 15:07:51 +08:00
* @memberof Konva.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;
},
clone: function(obj) {
if (!obj) {
obj = {};
}
2015-01-27 15:07:51 +08:00
obj.container = Konva.document.createElement(DIV);
2015-01-27 15:07:51 +08:00
return Konva.Container.prototype.clone.call(this, obj);
},
/**
* destroy stage
* @method
2015-01-27 15:07:51 +08:00
* @memberof Konva.Stage.prototype
*/
destroy: function() {
var content = this.content;
2015-01-27 15:07:51 +08:00
Konva.Container.prototype.destroy.call(this);
2015-01-27 15:07:51 +08:00
if(content && Konva.Util._isInDocument(content)) {
2013-03-24 14:50:51 +08:00
this.getContainer().removeChild(content);
}
2015-01-27 15:07:51 +08:00
var index = Konva.stages.indexOf(this);
2014-02-27 22:53:30 +08:00
if (index > -1) {
2015-01-27 15:07:51 +08:00
Konva.stages.splice(index, 1);
2014-02-27 22:53:30 +08:00
}
},
/**
2013-10-09 00:29:08 +08:00
* get pointer position which can be a touch position or mouse position
* @method
2015-01-27 15:07:51 +08:00
* @memberof Konva.Stage.prototype
2014-01-13 01:15:35 +08:00
* @returns {Object}
*/
getPointerPosition: function() {
2013-10-09 00:29:08 +08:00
return this.pointerPos;
},
getStage: function() {
return this;
},
/**
* get stage content div element which has the
2015-01-27 15:07:51 +08:00
* the class name "konvajs-content"
* @method
2015-01-27 15:07:51 +08:00
* @memberof Konva.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
2015-01-27 15:07:51 +08:00
* @memberof Konva.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,
2015-01-27 15:07:51 +08:00
canvas = new Konva.SceneCanvas({
width: config.width || this.getWidth(),
height: config.height || this.getHeight(),
pixelRatio: 1
}),
_context = canvas.getContext()._context,
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(),
2015-01-27 15:07:51 +08:00
imageObj = new Konva.window.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
2015-01-27 15:07:51 +08:00
* @memberof Konva.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) {
2015-01-27 15:07:51 +08:00
Konva.Util._getImage(dataUrl, function(img) {
cb(img);
});
};
this.toDataURL(config);
},
/**
2013-11-28 13:32:36 +08:00
* get visible intersection shape. This is the preferred
* method for determining if a point intersects a shape or not
* @method
2015-01-27 15:07:51 +08:00
* @memberof Konva.Stage.prototype
* @param {Object} pos
* @param {Number} pos.x
* @param {Number} pos.y
2015-01-27 15:07:51 +08:00
* @returns {Konva.Shape}
*/
getIntersection: function(pos) {
var layers = this.getChildren(),
2013-03-24 14:50:51 +08:00
len = layers.length,
end = len - 1,
2013-11-28 13:32:36 +08:00
n, shape;
2013-03-24 14:50:51 +08:00
for(n = end; n >= 0; n--) {
2013-11-28 13:32:36 +08:00
shape = layers[n].getIntersection(pos);
if (shape) {
return shape;
2014-02-27 08:49:18 +08:00
}
}
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);
2015-01-28 06:26:18 +08:00
// this.bufferCanvas2.setSize(width, height);
this.bufferHitCanvas.setSize(width, height);
// set layer dimensions
2013-03-24 14:50:51 +08:00
for(n = 0; n < len; n++) {
layer = layers[n];
layer.setSize(width, height);
layer.draw();
}
}
},
/**
* add layer or layers to stage
* @method
2015-01-27 15:07:51 +08:00
* @memberof Konva.Stage.prototype
* @param {...Konva.Layer} layer
* @example
* stage.add(layer1, layer2, layer3);
*/
2014-03-14 13:20:39 +08:00
add: function(layer) {
if (arguments.length > 1) {
for (var i = 0; i < arguments.length; i++) {
this.add(arguments[i]);
}
2014-03-14 13:20:39 +08:00
return;
}
2015-01-27 15:07:51 +08:00
Konva.Container.prototype.add.call(this, layer);
layer._setCanvasSize(this.width(), this.height());
// draw layer and append canvas to container
layer.draw();
this.content.appendChild(layer.canvas._canvas);
// chainable
return this;
},
getParent: function() {
return null;
},
getLayer: function() {
return null;
},
/**
2015-01-27 15:07:51 +08:00
* returns a {@link Konva.Collection} of layers
* @method
2015-01-27 15:07:51 +08:00
* @memberof Konva.Stage.prototype
*/
getLayers: function() {
return this.getChildren();
},
_bindContentEvents: function() {
2014-02-27 08:49:18 +08:00
for (var n = 0; n < eventsLength; n++) {
addEvent(this, EVENTS[n]);
}
},
_mouseover: function(evt) {
2015-01-27 15:07:51 +08:00
if (!Konva.UA.mobile) {
this._setPointerPosition(evt);
this._fire(CONTENT_MOUSEOVER, {evt: evt});
}
},
_mouseout: function(evt) {
2015-01-27 15:07:51 +08:00
if (!Konva.UA.mobile) {
this._setPointerPosition(evt);
var targetShape = this.targetShape;
2015-01-27 15:07:51 +08:00
if(targetShape && !Konva.isDragging()) {
targetShape._fireAndBubble(MOUSEOUT, {evt: evt});
targetShape._fireAndBubble(MOUSELEAVE, {evt: evt});
this.targetShape = null;
}
this.pointerPos = undefined;
this._fire(CONTENT_MOUSEOUT, {evt: evt});
}
},
_mousemove: function(evt) {
// workaround for mobile IE to force touch event when unhandled pointer event elevates into a mouse event
2015-01-27 15:07:51 +08:00
if (Konva.UA.ieMobile) {
return this._touchmove(evt);
}
// workaround fake mousemove event in chrome browser https://code.google.com/p/chromium/issues/detail?id=161464
if ((typeof evt.webkitMovementX !== 'undefined' || typeof evt.webkitMovementY !== 'undefined') && evt.webkitMovementY === 0 && evt.webkitMovementX === 0) {
return;
}
2015-01-27 15:07:51 +08:00
if (Konva.UA.mobile) {
return;
}
this._setPointerPosition(evt);
2015-01-27 15:07:51 +08:00
var dd = Konva.DD, shape;
2015-01-27 15:07:51 +08:00
if (!Konva.isDragging()) {
2013-11-28 13:32:36 +08:00
shape = this.getIntersection(this.getPointerPosition());
if(shape && shape.isListening()) {
2015-01-27 15:07:51 +08:00
if(!Konva.isDragging() && (!this.targetShape || this.targetShape._id !== shape._id)) {
if(this.targetShape) {
this.targetShape._fireAndBubble(MOUSEOUT, {evt: evt}, shape);
this.targetShape._fireAndBubble(MOUSELEAVE, {evt: evt}, shape);
}
shape._fireAndBubble(MOUSEOVER, {evt: evt}, this.targetShape);
shape._fireAndBubble(MOUSEENTER, {evt: evt}, this.targetShape);
this.targetShape = shape;
}
else {
shape._fireAndBubble(MOUSEMOVE, {evt: evt});
}
}
/*
* if no shape was detected, clear target shape and try
* to run mouseout from previous target shape
*/
else {
2015-01-27 15:07:51 +08:00
if(this.targetShape && !Konva.isDragging()) {
this.targetShape._fireAndBubble(MOUSEOUT, {evt: evt});
this.targetShape._fireAndBubble(MOUSELEAVE, {evt: evt});
2014-02-27 09:13:55 +08:00
this.targetShape = null;
}
}
// content event
this._fire(CONTENT_MOUSEMOVE, {evt: evt});
}
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) {
// workaround for mobile IE to force touch event when unhandled pointer event elevates into a mouse event
2015-01-27 15:07:51 +08:00
if (Konva.UA.ieMobile) {
return this._touchstart(evt);
}
2015-01-27 15:07:51 +08:00
if (!Konva.UA.mobile) {
this._setPointerPosition(evt);
var shape = this.getIntersection(this.getPointerPosition());
2015-01-27 15:07:51 +08:00
Konva.listenClickTap = true;
if (shape && shape.isListening()) {
this.clickStartShape = shape;
shape._fireAndBubble(MOUSEDOWN, {evt: evt});
}
// content event
this._fire(CONTENT_MOUSEDOWN, {evt: 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) {
2015-02-14 23:12:54 +08:00
// workaround for mobile IE to force touch event when unhandled pointer event elevates into a mouse event
2015-01-27 15:07:51 +08:00
if (Konva.UA.ieMobile) {
return this._touchend(evt);
}
2015-01-27 15:07:51 +08:00
if (!Konva.UA.mobile) {
this._setPointerPosition(evt);
var shape = this.getIntersection(this.getPointerPosition()),
clickStartShape = this.clickStartShape,
fireDblClick = false,
2015-01-27 15:07:51 +08:00
dd = Konva.DD;
2015-01-27 15:07:51 +08:00
if(Konva.inDblClickWindow) {
fireDblClick = true;
2015-01-27 15:07:51 +08:00
Konva.inDblClickWindow = false;
}
// don't set inDblClickWindow after dragging
else if (!dd || !dd.justDragged) {
2015-01-27 15:07:51 +08:00
Konva.inDblClickWindow = true;
} else if (dd) {
dd.justDragged = false;
}
setTimeout(function() {
2015-01-27 15:07:51 +08:00
Konva.inDblClickWindow = false;
}, Konva.dblClickWindow);
if (shape && shape.isListening()) {
shape._fireAndBubble(MOUSEUP, {evt: evt});
// detect if click or double click occurred
2015-01-27 15:07:51 +08:00
if(Konva.listenClickTap && clickStartShape && clickStartShape._id === shape._id) {
shape._fireAndBubble(CLICK, {evt: evt});
if(fireDblClick) {
shape._fireAndBubble(DBL_CLICK, {evt: evt});
}
}
}
// content events
this._fire(CONTENT_MOUSEUP, {evt: evt});
2015-01-27 15:07:51 +08:00
if (Konva.listenClickTap) {
this._fire(CONTENT_CLICK, {evt: evt});
if(fireDblClick) {
this._fire(CONTENT_DBL_CLICK, {evt: evt});
}
}
2015-01-27 15:07:51 +08:00
Konva.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);
2013-11-28 13:32:36 +08:00
var shape = this.getIntersection(this.getPointerPosition());
2015-01-27 15:07:51 +08:00
Konva.listenClickTap = true;
if (shape && shape.isListening()) {
this.tapStartShape = shape;
shape._fireAndBubble(TOUCHSTART, {evt: evt});
// only call preventDefault if the shape is listening for events
if (shape.isListening() && evt.preventDefault) {
evt.preventDefault();
}
}
// content event
this._fire(CONTENT_TOUCHSTART, {evt: evt});
},
_touchend: function(evt) {
this._setPointerPosition(evt);
2014-02-27 08:49:18 +08:00
var shape = this.getIntersection(this.getPointerPosition()),
fireDblClick = false;
2015-01-27 15:07:51 +08:00
if(Konva.inDblClickWindow) {
2014-02-27 08:49:18 +08:00
fireDblClick = true;
2015-01-27 15:07:51 +08:00
Konva.inDblClickWindow = false;
2014-02-27 08:49:18 +08:00
}
else {
2015-01-27 15:07:51 +08:00
Konva.inDblClickWindow = true;
2014-02-27 08:49:18 +08:00
}
2014-02-27 08:49:18 +08:00
setTimeout(function() {
2015-01-27 15:07:51 +08:00
Konva.inDblClickWindow = false;
}, Konva.dblClickWindow);
if (shape && shape.isListening()) {
shape._fireAndBubble(TOUCHEND, {evt: evt});
// detect if tap or double tap occurred
2015-01-27 15:07:51 +08:00
if(Konva.listenClickTap && shape._id === this.tapStartShape._id) {
shape._fireAndBubble(TAP, {evt: evt});
if(fireDblClick) {
shape._fireAndBubble(DBL_TAP, {evt: evt});
}
}
// only call preventDefault if the shape is listening for events
if (shape.isListening() && evt.preventDefault) {
evt.preventDefault();
}
}
// content events
2015-01-27 15:07:51 +08:00
if (Konva.listenClickTap) {
this._fire(CONTENT_TOUCHEND, {evt: evt});
if(fireDblClick) {
this._fire(CONTENT_DBL_TAP, {evt: evt});
}
}
2015-01-27 15:07:51 +08:00
Konva.listenClickTap = false;
},
_touchmove: function(evt) {
this._setPointerPosition(evt);
2015-01-27 15:07:51 +08:00
var dd = Konva.DD,
shape;
2015-01-27 15:07:51 +08:00
if (!Konva.isDragging()) {
2013-11-28 13:32:36 +08:00
shape = this.getIntersection(this.getPointerPosition());
if (shape && shape.isListening()) {
shape._fireAndBubble(TOUCHMOVE, {evt: evt});
// only call preventDefault if the shape is listening for events
if (shape.isListening() && evt.preventDefault) {
evt.preventDefault();
}
}
this._fire(CONTENT_TOUCHMOVE, {evt: evt});
}
if(dd) {
dd._drag(evt);
2015-01-27 15:07:51 +08:00
if (Konva.isDragging()) {
2014-05-03 12:11:34 +08:00
evt.preventDefault();
}
}
},
2014-05-01 21:11:03 +08:00
_DOMMouseScroll: function(evt) {
this._mousewheel(evt);
},
_mousewheel: function(evt) {
this._setPointerPosition(evt);
var shape = this.getIntersection(this.getPointerPosition());
if (shape && shape.isListening()) {
shape._fireAndBubble(MOUSEWHEEL, {evt: evt});
}
},
_wheel: function(evt) {
this._mousewheel(evt);
},
2013-10-09 00:29:08 +08:00
_setPointerPosition: function(evt) {
2014-02-27 08:49:18 +08:00
var contentPosition = this._getContentPosition(),
offsetX = evt.offsetX,
clientX = evt.clientX,
2013-11-08 16:30:26 +08:00
x = null,
y = null,
2013-10-09 00:29:08 +08:00
touch;
2014-02-27 08:49:18 +08:00
evt = evt ? evt : window.event;
2013-10-09 00:29:08 +08:00
// touch events
2013-11-08 16:30:26 +08:00
if(evt.touches !== undefined) {
// currently, only handle one finger
if (evt.touches.length > 0) {
2013-11-08 16:30:26 +08:00
touch = evt.touches[0];
// get the information for finger #1
x = touch.clientX - contentPosition.left;
2014-02-27 08:49:18 +08:00
y = touch.clientY - contentPosition.top;
2013-11-08 16:30:26 +08:00
}
}
2013-10-09 00:29:08 +08:00
// mouse events
else {
// if offsetX is defined, assume that offsetY is defined as well
if (offsetX !== undefined) {
x = offsetX;
y = evt.offsetY;
}
// we unfortunately have to use UA detection here because accessing
// the layerX or layerY properties in newer versions of Chrome
2013-10-09 00:29:08 +08:00
// throws a JS warning. layerX and layerY are required for FF
// when the container is transformed via CSS.
2015-01-27 15:07:51 +08:00
else if (Konva.UA.browser === 'mozilla') {
2015-02-09 08:21:55 +08:00
x = evt.layerX || (evt.clientX - contentPosition.left);
y = evt.layerY || (evt.clientY - contentPosition.top);
2013-10-09 00:29:08 +08:00
}
// if clientX is defined, assume that clientY is defined as well
else if (clientX !== undefined && contentPosition) {
x = clientX - contentPosition.left;
y = evt.clientY - contentPosition.top;
}
}
2013-11-08 16:30:26 +08:00
if (x !== null && y !== null) {
this.pointerPos = {
x: x,
y: y
};
}
},
_getContentPosition: function() {
var rect = this.content.getBoundingClientRect ? this.content.getBoundingClientRect() : { top: 0, left: 0 };
return {
top: rect.top,
left: rect.left
};
},
_buildDOM: function() {
var container = this.getContainer();
2014-03-01 18:09:27 +08:00
if (!container) {
2015-01-27 15:07:51 +08:00
if (Konva.Util.isBrowser()) {
throw 'Stage has no container. A container is required.';
2014-03-01 18:09:27 +08:00
} else {
// automatically create element for jsdom in nodejs env
2015-01-27 15:07:51 +08:00
container = Konva.document.createElement(DIV);
2014-03-01 18:09:27 +08:00
}
}
// clear content inside container
container.innerHTML = EMPTY_STRING;
// content
2015-01-27 15:07:51 +08:00
this.content = Konva.document.createElement(DIV);
2013-03-24 14:50:51 +08:00
this.content.style.position = RELATIVE;
this.content.className = KONVA_CONTENT;
this.content.setAttribute('role', 'presentation');
container.appendChild(this.content);
// the buffer canvas pixel ratio must be 1 because it is used as an
// intermediate canvas before copying the result onto a scene canvas.
// not setting it to 1 will result in an over compensation
2015-01-27 15:07:51 +08:00
this.bufferCanvas = new Konva.SceneCanvas({
pixelRatio: 1
});
2015-01-28 06:26:18 +08:00
// this.bufferCanvas2 = new Konva.SceneCanvas({
// pixelRatio: 1
// });
2015-01-27 15:07:51 +08:00
this.bufferHitCanvas = new Konva.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);
}
2014-03-15 16:58:26 +08:00
},
// currently cache function is now working for stage, because stage has no its own canvas element
// TODO: may be it is better to cache all children layers?
2014-03-15 16:58:26 +08:00
cache: function() {
2015-01-27 15:07:51 +08:00
Konva.Util.warn('Cache function is not allowed for stage. You may use cache only for layers, groups and shapes.');
2014-03-15 16:58:26 +08:00
},
clearCache : function() {
}
});
2015-01-27 15:07:51 +08:00
Konva.Util.extend(Konva.Stage, Konva.Container);
// add getters and setters
2015-01-27 15:07:51 +08:00
Konva.Factory.addGetter(Konva.Stage, 'container');
Konva.Factory.addOverloadedGetterSetter(Konva.Stage, 'container');
/**
* get container DOM element
2014-01-06 12:52:09 +08:00
* @name container
* @method
2015-01-27 15:07:51 +08:00
* @memberof Konva.Stage.prototype
2014-01-06 12:52:09 +08:00
* @returns {DomElement} container
* @example
2014-04-04 11:17:09 +08:00
* // get container
* var container = stage.container();
*
2014-04-04 11:17:09 +08:00
* // set container
* var container = document.createElement('div');
* body.appendChild(container);
* stage.container(container);
*/
})();