This commit is contained in:
Anton Lavrenov
2020-04-07 08:46:50 -05:00
parent 55c812425d
commit 1427cf0efc
12 changed files with 1380 additions and 115 deletions

View File

@@ -1,11 +1,11 @@
import { Util, Collection, Point } from './Util';
import { Util, Collection } from './Util';
import { Container, ContainerConfig } from './Container';
import { Node } from './Node';
import { Factory } from './Factory';
import { SceneCanvas, HitCanvas } from './Canvas';
import { Stage } from './Stage';
import { GetSet } from './types';
import { GetSet, Vector2d } from './types';
import { Group } from './Group';
import { Shape } from './Shape';
@@ -255,7 +255,7 @@ export abstract class BaseLayer extends Container<Group | Shape> {
'Can not change height of layer. Use "stage.height(value)" function instead.'
);
}
getIntersection(pos: Point, selector?: string) {
getIntersection(pos: Vector2d, selector?: string) {
return null;
}

View File

@@ -1,4 +1,4 @@
import { Util, Collection, Point } from './Util';
import { Util, Collection } from './Util';
import { Container } from './Container';
import { Factory } from './Factory';
import { BaseLayer } from './BaseLayer';
@@ -7,7 +7,7 @@ import { shapes } from './Shape';
import { getBooleanValidator } from './Validators';
import { _registerNode } from './Global';
import { GetSet } from './types';
import { GetSet, Vector2d } from './types';
// constants
var HASH = '#',
@@ -77,7 +77,7 @@ export class Layer extends BaseLayer {
* // or if you interested in shape parent:
* var group = layer.getIntersection({x: 50, y: 50}, 'Group');
*/
getIntersection(pos: Point, selector?: string) {
getIntersection(pos: Vector2d, selector?: string) {
var obj, i, intersectionOffset, shape;
if (!this.hitGraphEnabled() || !this.isVisible()) {

View File

@@ -1,4 +1,4 @@
import { Util, Collection, Transform, RectConf, Point } from './Util';
import { Util, Collection, Transform } from './Util';
import { Factory } from './Factory';
import { SceneCanvas, HitCanvas, Canvas } from './Canvas';
import { Konva, _NODES_REGISTRY } from './Global';
@@ -143,7 +143,6 @@ var ABSOLUTE_OPACITY = 'absoluteOpacity',
TRANSFORM = 'transform',
UPPER_STAGE = 'Stage',
VISIBLE = 'visible',
CLONE_BLACK_LIST = ['id'],
TRANSFORM_CHANGE_STR = [
'xChange.konva',
'yChange.konva',
@@ -198,7 +197,7 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
index = 0;
parent: Container<Node> | null = null;
_cache: Map<string, any> = new Map<string, any>();
_lastPos: Point = null;
_lastPos: Vector2d = null;
_attrsAffectingSize!: string[];
_filterUpToDate = false;
@@ -347,7 +346,7 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
imageSmoothingEnabled?: boolean;
}) {
var conf = config || {};
var rect = {} as RectConf;
var rect = {} as IRect;
// don't call getClientRect if we have all attributes
// it means call it only if have one undefined

View File

@@ -1,18 +1,7 @@
import { glob, Konva } from './Global';
import { Node } from './Node';
import { IRect, RGB, RGBA } from './types';
import { IRect, RGB, RGBA, Vector2d } from './types';
export type Point = {
x: number;
y: number;
};
export interface RectConf {
x: number;
y: number;
width: number;
height: number;
}
/**
* Collection constructor. Collection extends Array.
@@ -163,7 +152,7 @@ export class Transform {
* @param {Object} point 2D point(x, y)
* @returns {Object} 2D point(x, y)
*/
point(point: Point) {
point(point: Vector2d) {
var m = this.m;
return {
x: m[0] * point.x + m[2] * point.y + m[4],
@@ -1025,7 +1014,7 @@ export const Util = {
},
// line as array of points.
// line might be closed
_getProjectionToLine(pt: Point, line, isClosed) {
_getProjectionToLine(pt: Vector2d, line, isClosed) {
var pc = Util.cloneObject(pt);
var dist = Number.MAX_VALUE;
line.forEach(function(p1, i) {

View File

@@ -1,4 +1,4 @@
import { Util, Collection, Transform, Point } from '../Util';
import { Util, Collection, Transform } from '../Util';
import { Factory } from '../Factory';
import { Node } from '../Node';
import { Shape } from '../Shape';
@@ -9,7 +9,7 @@ import { Konva } from '../Global';
import { getNumberValidator } from '../Validators';
import { _registerNode } from '../Global';
import { GetSet, IRect } from '../types';
import { GetSet, IRect, Vector2d } from '../types';
interface Box extends IRect {
rotation: number;
@@ -145,15 +145,7 @@ var ANCHORS_NAMES = [
var MAX_SAFE_INTEGER = 100000000;
type SHAPE = {
x: number;
y: number;
width: number;
height: number;
rotation: number;
};
function getCenter(shape: SHAPE) {
function getCenter(shape: Box) {
return {
x:
shape.x +
@@ -166,7 +158,7 @@ function getCenter(shape: SHAPE) {
};
}
function rotateAroundPoint(shape: SHAPE, angleRad: number, point: Point) {
function rotateAroundPoint(shape: Box, angleRad: number, point: Vector2d) {
const x =
point.x +
(shape.x - point.x) * Math.cos(angleRad) -
@@ -183,12 +175,12 @@ function rotateAroundPoint(shape: SHAPE, angleRad: number, point: Point) {
};
}
function rotateAroundCenter(shape: SHAPE, deltaRad: number) {
function rotateAroundCenter(shape: Box, deltaRad: number) {
const center = getCenter(shape);
return rotateAroundPoint(shape, deltaRad, center);
}
function getShapeRect(shape: SHAPE) {
function getShapeRect(shape: Box) {
const angleRad = shape.rotation;
const x1 = shape.x;
const y1 = shape.y;
@@ -217,7 +209,7 @@ function getShapeRect(shape: SHAPE) {
};
}
function getShapesRect(shapes: Array<SHAPE>) {
function getShapesRect(shapes: Array<Box>) {
// if (shapes.length === 1) {
// const shape = shapes[0];
@@ -251,9 +243,9 @@ function getShapesRect(shapes: Array<SHAPE>) {
}
function transformShape(
shape: SHAPE,
oldSelection: SHAPE,
newSelection: SHAPE,
shape: Box,
oldSelection: Box,
newSelection: Box,
keepOffset = 1
) {
const offset = rotateAroundPoint(shape, -oldSelection.rotation, {
@@ -284,9 +276,9 @@ function transformShape(
}
function transformAndRotateShape(
shape: SHAPE,
oldSelection: SHAPE,
newSelection: SHAPE
shape: Box,
oldSelection: Box,
newSelection: Box
) {
const updated = transformShape(shape, oldSelection, newSelection);
return rotateAroundPoint(
@@ -337,7 +329,7 @@ export class Transformer extends Group {
_nodes: Array<Node>;
_movingAnchorName: string;
_transforming = false;
_anchorDragOffset: Point;
_anchorDragOffset: Vector2d;
sin: number;
cos: number;
_cursorChange: boolean;

View File

@@ -1,6 +1,3 @@
import { Shape } from './Shape';
import { Stage } from './Stage';
export interface GetSet<Type, This> {
(): Type;
(v: Type): This;
@@ -19,13 +16,13 @@ export interface IRect {
}
export interface IFrame {
time: number;
timeDiff: number;
lastTime: number;
frameRate: number;
time: number;
timeDiff: number;
lastTime: number;
frameRate: number;
}
export type AnimationFn = (frame?: IFrame) => boolean|void;
export type AnimationFn = (frame?: IFrame) => boolean | void;
export enum KonvaNodeEvent {
mouseover = 'mouseover',
@@ -57,4 +54,4 @@ export interface RGB {
export interface RGBA extends RGB {
a: number;
}
}