refactoring

This commit is contained in:
Anton Lavrenov 2019-02-23 20:54:20 -05:00
parent 3e7503ebdc
commit 98afa3fac6
16 changed files with 504 additions and 498 deletions

View File

@ -7,25 +7,24 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## Breaking ## Breaking
It is not possible to make custom build of `Konva` from npm installation, like `import Konva from 'konva/src/Core';` Customs builds are removed from npm package. You can not use `import Konva from 'konva/src/Core';`.
The issue will be resolved later. This feature will be added back later.
### Possibly breaking ### Possibly breaking
That changes are private and internal specific. They should not break most of `Konva` apps. That changes are private and internal specific. They should not break most of `Konva` apps.
* `Konva.Util.addMethods` * `Konva.Util.addMethods` is removed
* `Konva.Util._removeLastLetter` * `Konva.Util._removeLastLetter` is removed
* `Konva.Util._getImage` * `Konva.Util._getImage` is removed
* `Konv.Util._getRGBAString` * `Konv.Util._getRGBAString` is removed
* `Konv.Util._merge` * `Konv.Util._merge` is removed
* Removed polyfill for `requestAnimationFrame`. * Removed polyfill for `requestAnimationFrame`.
* `id` and `name` properties defaults are empty strings, not `undefined` * `id` and `name` properties defaults are empty strings, not `undefined`
* internal `_cache` property was updated to use es2015 `Map` instead of `{}`. * internal `_cache` property was updated to use es2015 `Map` instead of `{}`.
### Added ### Added
* Show a warning when a stage has too many layers * Show a warning when a stage has too many layers
* Show a warning on duplicate ids * Show a warning on duplicate ids
@ -627,8 +626,8 @@ That changes are private and internal specific. They should not break most of `K
### Added ### Added
* new methods for working with node's name: `addName`, `removeName`, `hasName`. * new methods for working with node's name: `addName`, `removeName`, `hasName`.
* new `perfectDrawEnabled` property for shape. See [http://konvajs.github.io/docs/performance/Disable_Perfect_Draw.html](http://konvajs.github.io/docs/performance/Disable_Perfect_Draw.html) * new `perfectDrawEnabled` property for shape. See [http://konvajs.org/docs/performance/Disable_Perfect_Draw.html](http://konvajs.org/docs/performance/Disable_Perfect_Draw.html)
* new `shadowForStrokeEnabled` property for shape. See [http://konvajs.github.io/docs/performance/All_Performance_Tips.html](http://konvajs.github.io/docs/performance/All_Performance_Tips.html) * new `shadowForStrokeEnabled` property for shape. See [http://konvajs.org/docs/performance/All_Performance_Tips.html](http://konvajs.org/docs/performance/All_Performance_Tips.html)
* new `getClientRect` method. * new `getClientRect` method.
* new `to` method for every node for shorter tweening * new `to` method for every node for shorter tweening

View File

@ -16,8 +16,8 @@ You can draw things onto the stage, add event listeners to them, move them, scal
This repository began as a GitHub fork of [ericdrowell/KineticJS](https://github.com/ericdrowell/KineticJS). This repository began as a GitHub fork of [ericdrowell/KineticJS](https://github.com/ericdrowell/KineticJS).
* **Visit:** The [Home Page](http://konvajs.github.io/) and follow on [Twitter](https://twitter.com/lavrton) * **Visit:** The [Home Page](http://konvajs.org/) and follow on [Twitter](https://twitter.com/lavrton)
* **Discover:** [Tutorials](http://konvajs.github.io/docs), [API Documentation](http://konvajs.github.io/api) * **Discover:** [Tutorials](http://konvajs.org/docs), [API Documentation](http://konvajs.org/api)
* **Help:** [StackOverflow](http://stackoverflow.com/questions/tagged/konvajs), [Chat](https://gitter.im/konvajs/konva) * **Help:** [StackOverflow](http://stackoverflow.com/questions/tagged/konvajs), [Chat](https://gitter.im/konvajs/konva)
# Quick Look # Quick Look

View File

@ -3,7 +3,7 @@
"authors": [ "authors": [
"Anton Lavrenov", "Eric Rowell" "Anton Lavrenov", "Eric Rowell"
], ],
"homepage": "http://konvajs.github.io", "homepage": "http://konvajs.org",
"description": "Konva is an HTML5 Canvas JavaScript framework that enables high performance animations, transitions, node nesting, layering, filtering, caching, event handling for desktop and mobile applications, and much more.", "description": "Konva is an HTML5 Canvas JavaScript framework that enables high performance animations, transitions, node nesting, layering, filtering, caching, event handling for desktop and mobile applications, and much more.",
"keywords": [ "keywords": [
"canvas", "canvas",

View File

@ -18,7 +18,7 @@
"bugs": { "bugs": {
"url": "https://github.com/konvajs/konva/issues" "url": "https://github.com/konvajs/konva/issues"
}, },
"homepage": "http://konvajs.github.io/", "homepage": "http://konvajs.org/",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git://github.com/konvajs/konva.git" "url": "git://github.com/konvajs/konva.git"

853
konva.js

File diff suppressed because it is too large Load Diff

4
konva.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -57,7 +57,7 @@
"bugs": { "bugs": {
"url": "https://github.com/konvajs/konva/issues" "url": "https://github.com/konvajs/konva/issues"
}, },
"homepage": "http://konvajs.github.io/", "homepage": "http://konvajs.org/",
"readmeFilename": "README.md", "readmeFilename": "README.md",
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -1,7 +1,7 @@
import { Util, Collection } from './Util'; import { Util, Collection } from './Util';
import { Factory, Validators } from './Factory'; import { Factory, Validators } from './Factory';
import { Node, ids } from './Node'; import { Node, ids, names } from './Node';
import { getGlobalKonva, names } from './Global'; import { getGlobalKonva } from './Global';
import { GetSet, IRect } from './types'; import { GetSet, IRect } from './types';

View File

@ -64,6 +64,9 @@ var CONTEXT_PROPERTIES = [
'globalCompositeOperation' 'globalCompositeOperation'
]; ];
// TODO: document all context methods
const traceArrMax = 100;
/** /**
* Konva wrapper around native 2d canvas context. It has almost the same API of 2d context with some additional functions. * Konva wrapper around native 2d canvas context. It has almost the same API of 2d context with some additional functions.
* With core Konva shapes you don't need to use this object. But you have to use it if you want to create * With core Konva shapes you don't need to use this object. But you have to use it if you want to create
@ -196,7 +199,7 @@ export class Context {
traceArr.push(str); traceArr.push(str);
len = traceArr.length; len = traceArr.length;
if (len >= getGlobalKonva().traceArrMax) { if (len >= traceArrMax) {
traceArr.shift(); traceArr.shift();
} }
} }

View File

@ -1,6 +1,9 @@
import { Animation } from './Animation'; import { Animation } from './Animation';
import { isBrowser, getGlobalKonva } from './Global'; import { isBrowser, getGlobalKonva } from './Global';
// TODO: make better module,
// make sure other modules import it without global
export const DD = { export const DD = {
startPointerPos: { startPointerPos: {
x: 0, x: 0,

View File

@ -1,6 +1,6 @@
/* /*
* Konva JavaScript Framework v@@version * Konva JavaScript Framework v@@version
* http://konvajs.github.io/ * http://konvajs.org/
* Licensed under the MIT * Licensed under the MIT
* Date: @@date * Date: @@date
* *
@ -15,9 +15,6 @@ var PI_OVER_180 = Math.PI / 180;
*/ */
export const version = '@@version'; export const version = '@@version';
export const names = {};
export const shapes = {};
export const isBrowser = export const isBrowser =
typeof window !== 'undefined' && typeof window !== 'undefined' &&
// browser case // browser case
@ -65,34 +62,6 @@ export const isDragReady = function() {
return false; return false;
}; };
export const _addName = function(node: any, name) {
if (name) {
if (!names[name]) {
names[name] = [];
}
names[name].push(node);
}
};
export const _removeName = function(name, _id) {
if (!name) {
return;
}
var nodes = names[name];
if (!nodes) {
return;
}
for (var n = 0; n < nodes.length; n++) {
var no = nodes[n];
if (no._id === _id) {
nodes.splice(n, 1);
}
}
if (nodes.length === 0) {
delete names[name];
}
};
export const getAngle = function(angle) { export const getAngle = function(angle) {
return getGlobalKonva().angleDeg ? angle * PI_OVER_180 : angle; return getGlobalKonva().angleDeg ? angle * PI_OVER_180 : angle;
}; };

View File

@ -3,7 +3,7 @@ import { Container } from './Container';
import { Factory, Validators } from './Factory'; import { Factory, Validators } from './Factory';
import { BaseLayer } from './BaseLayer'; import { BaseLayer } from './BaseLayer';
import { HitCanvas } from './Canvas'; import { HitCanvas } from './Canvas';
import { shapes } from './Global'; import { shapes } from './Shape';
import { GetSet } from './types'; import { GetSet } from './types';

View File

@ -1,12 +1,13 @@
import { Util, Collection, Transform, RectConf, Point } from './Util'; import { Util, Collection, Transform, RectConf, Point } from './Util';
import { Factory, Validators } from './Factory'; import { Factory, Validators } from './Factory';
import { SceneCanvas, HitCanvas } from './Canvas'; import { SceneCanvas, HitCanvas } from './Canvas';
import { _removeName, _addName, getGlobalKonva } from './Global'; import { getGlobalKonva } from './Global';
import { Container } from './Container'; import { Container } from './Container';
import { GetSet, Vector2d } from './types'; import { GetSet, Vector2d } from './types';
import { DD } from './DragAndDrop'; import { DD } from './DragAndDrop';
export const ids = {}; export const ids = {};
export const names = {};
const ID_WARNING = `Duplicate id "{id}". const ID_WARNING = `Duplicate id "{id}".
Please do not use same id several times, it will break find() method look up. Please do not use same id several times, it will break find() method look up.
@ -35,6 +36,34 @@ export const _removeId = function(id: string, node: any) {
delete ids[id]; delete ids[id];
}; };
export const _addName = function(node: any, name) {
if (name) {
if (!names[name]) {
names[name] = [];
}
names[name].push(node);
}
};
export const _removeName = function(name, _id) {
if (!name) {
return;
}
var nodes = names[name];
if (!nodes) {
return;
}
for (var n = 0; n < nodes.length; n++) {
var no = nodes[n];
if (no._id === _id) {
nodes.splice(n, 1);
}
}
if (nodes.length === 0) {
delete names[name];
}
};
export type Filter = (this: Node, imageData: ImageData) => void; export type Filter = (this: Node, imageData: ImageData) => void;
type globalCompositeOperationType = type globalCompositeOperationType =
@ -215,7 +244,7 @@ export abstract class Node {
* cache node to improve drawing performance, apply filters, or create more accurate * cache node to improve drawing performance, apply filters, or create more accurate
* hit regions. For all basic shapes size of cache canvas will be automatically detected. * hit regions. For all basic shapes size of cache canvas will be automatically detected.
* If you need to cache your custom `Konva.Shape` instance you have to pass shape's bounding box * If you need to cache your custom `Konva.Shape` instance you have to pass shape's bounding box
* properties. Look at [https://konvajs.github.io/docs/performance/Shape_Caching.html](https://konvajs.github.io/docs/performance/Shape_Caching.html) for more information. * properties. Look at [https://konvajs.org/docs/performance/Shape_Caching.html](https://konvajs.org/docs/performance/Shape_Caching.html) for more information.
* @method * @method
* @name Konva.Node#cache * @name Konva.Node#cache
* @param {Object} [config] * @param {Object} [config]

View File

@ -1,6 +1,5 @@
import { Util, Collection } from './Util'; import { Util, Collection } from './Util';
import { Factory, Validators } from './Factory'; import { Factory, Validators } from './Factory';
import { shapes } from './Global';
import { Node } from './Node'; import { Node } from './Node';
import { GetSet, Vector2d } from './types'; import { GetSet, Vector2d } from './types';
@ -21,6 +20,8 @@ function getDummyContext() {
return dummyContext; return dummyContext;
} }
export const shapes = {};
// TODO: idea - use only "remove" (or destroy method) // TODO: idea - use only "remove" (or destroy method)
// how? on add, check that every inner shape has reference in konva store with color // how? on add, check that every inner shape has reference in konva store with color
// on remove - clear that reference // on remove - clear that reference
@ -783,7 +784,7 @@ Factory.addGetterSetter(
/** /**
* get/set perfectDrawEnabled. If a shape has fill, stroke and opacity you may set `perfectDrawEnabled` to false to improve performance. * get/set perfectDrawEnabled. If a shape has fill, stroke and opacity you may set `perfectDrawEnabled` to false to improve performance.
* See http://konvajs.github.io/docs/performance/Disable_Perfect_Draw.html for more information. * See http://konvajs.org/docs/performance/Disable_Perfect_Draw.html for more information.
* Default value is true * Default value is true
* @name Konva.Shape#perfectDrawEnabled * @name Konva.Shape#perfectDrawEnabled
* @method * @method

View File

@ -1,7 +1,7 @@
export * from './Global'; export * from './Global';
export { Collection, Util } from './Util'; export { Collection, Util } from './Util';
export { Node, ids } from './Node'; export { Node, ids, names } from './Node';
export { Container } from './Container'; export { Container } from './Container';
export { Stage, stages } from './Stage'; export { Stage, stages } from './Stage';
@ -12,13 +12,14 @@ export { FastLayer } from './FastLayer';
export { Group } from './Group'; export { Group } from './Group';
export { DD } from './DragAndDrop'; export { DD } from './DragAndDrop';
export { Shape } from './Shape'; export { Shape, shapes } from './Shape';
export { Animation } from './Animation'; export { Animation } from './Animation';
export { Tween, Easings } from './Tween'; export { Tween, Easings } from './Tween';
export const enableTrace = false; export const enableTrace = false;
export const traceArrMax = 100;
// TODO: move that to stage?
export const listenClickTap = false; export const listenClickTap = false;
export const inDblClickWindow = false; export const inDblClickWindow = false;
@ -97,8 +98,8 @@ export { Text } from './shapes/Text';
export { TextPath } from './shapes/TextPath'; export { TextPath } from './shapes/TextPath';
export { Transformer } from './shapes/Transformer'; export { Transformer } from './shapes/Transformer';
export { Wedge } from './shapes/Wedge'; export { Wedge } from './shapes/Wedge';
// filters
// filters
import { Blur } from './filters/Blur'; import { Blur } from './filters/Blur';
import { Brighten } from './filters/Brighten'; import { Brighten } from './filters/Brighten';
import { Contrast } from './filters/Contrast'; import { Contrast } from './filters/Contrast';

View File

@ -55,10 +55,10 @@ function getDummyContext() {
} }
function _fillFunc(context) { function _fillFunc(context) {
context.fillText(this.partialText, this._textX, this._textY); context.fillText(this._partialText, this._partialTextX, this._partialTextY);
} }
function _strokeFunc(context) { function _strokeFunc(context) {
context.strokeText(this.partialText, this._textX, this._textY); context.strokeText(this._partialText, this._partialTextX, this._partialTextY);
} }
function checkDefaultFill(config) { function checkDefaultFill(config) {
@ -107,9 +107,9 @@ function checkDefaultFill(config) {
*/ */
export class Text extends Shape { export class Text extends Shape {
textArr: Array<{ text: string; width: number }>; textArr: Array<{ text: string; width: number }>;
partialText: string; _partialText: string;
_textX = 0; _partialTextX = 0;
_textY = 0; _partialTextY = 0;
textWidth: number; textWidth: number;
textHeight: number; textHeight: number;
@ -240,17 +240,17 @@ export class Text extends Shape {
// 0 // 0
// ); // );
} }
this._textX = lineTranslateX; this._partialTextX = lineTranslateX;
this._textY = translateY + lineTranslateY; this._partialTextY = translateY + lineTranslateY;
this.partialText = letter; this._partialText = letter;
context.fillStrokeShape(this); context.fillStrokeShape(this);
lineTranslateX += lineTranslateX +=
Math.round(this.measureSize(letter).width) + letterSpacing; Math.round(this.measureSize(letter).width) + letterSpacing;
} }
} else { } else {
this._textX = lineTranslateX; this._partialTextX = lineTranslateX;
this._textY = translateY + lineTranslateY; this._partialTextY = translateY + lineTranslateY;
this.partialText = text; this._partialText = text;
context.fillStrokeShape(this); context.fillStrokeShape(this);
} }