UNPKG

konva

Version:

<p align="center"> <img src="https://raw.githubusercontent.com/konvajs/konvajs.github.io/master/apple-touch-icon-180x180.png" alt="Konva logo" height="180" /> </p>

173 lines (172 loc) 6.08 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var Util_1 = require("./Util"); var Container_1 = require("./Container"); var Factory_1 = require("./Factory"); var BaseLayer_1 = require("./BaseLayer"); var Canvas_1 = require("./Canvas"); var Shape_1 = require("./Shape"); var Validators_1 = require("./Validators"); var Global_1 = require("./Global"); var HASH = '#', BEFORE_DRAW = 'beforeDraw', DRAW = 'draw', INTERSECTION_OFFSETS = [ { x: 0, y: 0 }, { x: -1, y: -1 }, { x: 1, y: -1 }, { x: 1, y: 1 }, { x: -1, y: 1 } ], INTERSECTION_OFFSETS_LEN = INTERSECTION_OFFSETS.length; var Layer = (function (_super) { __extends(Layer, _super); function Layer() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.hitCanvas = new Canvas_1.HitCanvas({ pixelRatio: 1 }); return _this; } Layer.prototype._setCanvasSize = function (width, height) { this.canvas.setSize(width, height); this.hitCanvas.setSize(width, height); this._checkSmooth(); }; Layer.prototype._validateAdd = function (child) { var type = child.getType(); if (type !== 'Group' && type !== 'Shape') { Util_1.Util.throw('You may only add groups and shapes to a layer.'); } }; Layer.prototype.getIntersection = function (pos, selector) { var obj, i, intersectionOffset, shape; if (!this.hitGraphEnabled() || !this.isVisible()) { return null; } var spiralSearchDistance = 1; var continueSearch = false; while (true) { for (i = 0; i < INTERSECTION_OFFSETS_LEN; i++) { intersectionOffset = INTERSECTION_OFFSETS[i]; obj = this._getIntersection({ x: pos.x + intersectionOffset.x * spiralSearchDistance, y: pos.y + intersectionOffset.y * spiralSearchDistance }); shape = obj.shape; if (shape && selector) { return shape.findAncestor(selector, true); } else if (shape) { return shape; } continueSearch = !!obj.antialiased; if (!obj.antialiased) { break; } } if (continueSearch) { spiralSearchDistance += 1; } else { return null; } } }; Layer.prototype._getIntersection = function (pos) { var ratio = this.hitCanvas.pixelRatio; var p = this.hitCanvas.context.getImageData(Math.round(pos.x * ratio), Math.round(pos.y * ratio), 1, 1).data, p3 = p[3], colorKey, shape; if (p3 === 255) { colorKey = Util_1.Util._rgbToHex(p[0], p[1], p[2]); shape = Shape_1.shapes[HASH + colorKey]; if (shape) { return { shape: shape }; } return { antialiased: true }; } else if (p3 > 0) { return { antialiased: true }; } return {}; }; Layer.prototype.drawScene = function (can, top) { var layer = this.getLayer(), canvas = can || (layer && layer.getCanvas()); this._fire(BEFORE_DRAW, { node: this }); if (this.clearBeforeDraw()) { canvas.getContext().clear(); } Container_1.Container.prototype.drawScene.call(this, canvas, top); this._fire(DRAW, { node: this }); return this; }; Layer.prototype.drawHit = function (can, top) { var layer = this.getLayer(), canvas = can || (layer && layer.hitCanvas); if (layer && layer.clearBeforeDraw()) { layer .getHitCanvas() .getContext() .clear(); } Container_1.Container.prototype.drawHit.call(this, canvas, top); return this; }; Layer.prototype.clear = function (bounds) { BaseLayer_1.BaseLayer.prototype.clear.call(this, bounds); this.getHitCanvas() .getContext() .clear(bounds); return this; }; Layer.prototype.enableHitGraph = function () { this.hitGraphEnabled(true); return this; }; Layer.prototype.disableHitGraph = function () { this.hitGraphEnabled(false); return this; }; Layer.prototype.toggleHitCanvas = function () { if (!this.parent) { return; } var parent = this.parent; var added = !!this.hitCanvas._canvas.parentNode; if (added) { parent.content.removeChild(this.hitCanvas._canvas); } else { parent.content.appendChild(this.hitCanvas._canvas); } }; Layer.prototype.setSize = function (_a) { var width = _a.width, height = _a.height; _super.prototype.setSize.call(this, { width: width, height: height }); this.hitCanvas.setSize(width, height); return this; }; return Layer; }(BaseLayer_1.BaseLayer)); exports.Layer = Layer; Layer.prototype.nodeType = 'Layer'; Global_1._registerNode(Layer); Factory_1.Factory.addGetterSetter(Layer, 'hitGraphEnabled', true, Validators_1.getBooleanValidator()); Util_1.Collection.mapMethods(Layer);