mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 15:23:44 +08:00
extend transformer.anchorSize to support an array of width and height
This commit is contained in:
parent
f0e18b0907
commit
c07e8d017d
@ -6,7 +6,7 @@ import { Rect } from './Rect';
|
|||||||
import { Group } from '../Group';
|
import { Group } from '../Group';
|
||||||
import { ContainerConfig } from '../Container';
|
import { ContainerConfig } from '../Container';
|
||||||
import { Konva } from '../Global';
|
import { Konva } from '../Global';
|
||||||
import { getBooleanValidator, getNumberValidator } from '../Validators';
|
import { getBooleanValidator, getNumberValidator, getNumberOrArrayOfNumbersValidator } from '../Validators';
|
||||||
import { _registerNode } from '../Global';
|
import { _registerNode } from '../Global';
|
||||||
|
|
||||||
import { GetSet, IRect, Vector2d } from '../types';
|
import { GetSet, IRect, Vector2d } from '../types';
|
||||||
@ -28,7 +28,7 @@ export interface TransformerConfig extends ContainerConfig {
|
|||||||
anchorFill?: string;
|
anchorFill?: string;
|
||||||
anchorStroke?: string;
|
anchorStroke?: string;
|
||||||
anchorStrokeWidth?: number;
|
anchorStrokeWidth?: number;
|
||||||
anchorSize?: number;
|
anchorSize?: number | Array<number>;
|
||||||
anchorCornerRadius?: number;
|
anchorCornerRadius?: number;
|
||||||
keepRatio?: boolean;
|
keepRatio?: boolean;
|
||||||
centeredScaling?: boolean;
|
centeredScaling?: boolean;
|
||||||
@ -214,7 +214,7 @@ function getSnap(snaps: Array<number>, newRotationRad: number, tol: number) {
|
|||||||
* @param {String} [config.anchorStroke] Anchor stroke color
|
* @param {String} [config.anchorStroke] Anchor stroke color
|
||||||
* @param {String} [config.anchorCornerRadius] Anchor corner radius
|
* @param {String} [config.anchorCornerRadius] Anchor corner radius
|
||||||
* @param {Number} [config.anchorStrokeWidth] Anchor stroke size
|
* @param {Number} [config.anchorStrokeWidth] Anchor stroke size
|
||||||
* @param {Number} [config.anchorSize] Default is 10
|
* @param {Number} [config.anchorSize] Default is 10, can be an array specifying width, height
|
||||||
* @param {Boolean} [config.keepRatio] Should we keep ratio when we are moving edges? Default is true
|
* @param {Boolean} [config.keepRatio] Should we keep ratio when we are moving edges? Default is true
|
||||||
* @param {Boolean} [config.centeredScaling] Should we resize relative to node's center? Default is false
|
* @param {Boolean} [config.centeredScaling] Should we resize relative to node's center? Default is false
|
||||||
* @param {Array} [config.enabledAnchors] Array of names of enabled handles
|
* @param {Array} [config.enabledAnchors] Array of names of enabled handles
|
||||||
@ -1122,12 +1122,14 @@ export class Transformer extends Group {
|
|||||||
var padding = this.padding();
|
var padding = this.padding();
|
||||||
|
|
||||||
var anchorSize = this.anchorSize();
|
var anchorSize = this.anchorSize();
|
||||||
|
var anchorHeight = (anchorSize instanceof Array) ? anchorSize[1] : anchorSize;
|
||||||
|
var anchorWidth = (anchorSize instanceof Array) ? anchorSize[0] : anchorSize;
|
||||||
this.find('._anchor').forEach((node) => {
|
this.find('._anchor').forEach((node) => {
|
||||||
node.setAttrs({
|
node.setAttrs({
|
||||||
width: anchorSize,
|
width: anchorWidth,
|
||||||
height: anchorSize,
|
height: anchorHeight,
|
||||||
offsetX: anchorSize / 2,
|
offsetX: anchorWidth / 2,
|
||||||
offsetY: anchorSize / 2,
|
offsetY: anchorHeight / 2,
|
||||||
stroke: this.anchorStroke(),
|
stroke: this.anchorStroke(),
|
||||||
strokeWidth: this.anchorStrokeWidth(),
|
strokeWidth: this.anchorStrokeWidth(),
|
||||||
fill: this.anchorFill(),
|
fill: this.anchorFill(),
|
||||||
@ -1138,53 +1140,53 @@ export class Transformer extends Group {
|
|||||||
this._batchChangeChild('.top-left', {
|
this._batchChangeChild('.top-left', {
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0,
|
y: 0,
|
||||||
offsetX: anchorSize / 2 + padding,
|
offsetX: anchorWidth / 2 + padding,
|
||||||
offsetY: anchorSize / 2 + padding,
|
offsetY: anchorHeight / 2 + padding,
|
||||||
visible: resizeEnabled && enabledAnchors.indexOf('top-left') >= 0,
|
visible: resizeEnabled && enabledAnchors.indexOf('top-left') >= 0,
|
||||||
});
|
});
|
||||||
this._batchChangeChild('.top-center', {
|
this._batchChangeChild('.top-center', {
|
||||||
x: width / 2,
|
x: width / 2,
|
||||||
y: 0,
|
y: 0,
|
||||||
offsetY: anchorSize / 2 + padding,
|
offsetY: anchorHeight / 2 + padding,
|
||||||
visible: resizeEnabled && enabledAnchors.indexOf('top-center') >= 0,
|
visible: resizeEnabled && enabledAnchors.indexOf('top-center') >= 0,
|
||||||
});
|
});
|
||||||
this._batchChangeChild('.top-right', {
|
this._batchChangeChild('.top-right', {
|
||||||
x: width,
|
x: width,
|
||||||
y: 0,
|
y: 0,
|
||||||
offsetX: anchorSize / 2 - padding,
|
offsetX: anchorWidth / 2 - padding,
|
||||||
offsetY: anchorSize / 2 + padding,
|
offsetY: anchorHeight / 2 + padding,
|
||||||
visible: resizeEnabled && enabledAnchors.indexOf('top-right') >= 0,
|
visible: resizeEnabled && enabledAnchors.indexOf('top-right') >= 0,
|
||||||
});
|
});
|
||||||
this._batchChangeChild('.middle-left', {
|
this._batchChangeChild('.middle-left', {
|
||||||
x: 0,
|
x: 0,
|
||||||
y: height / 2,
|
y: height / 2,
|
||||||
offsetX: anchorSize / 2 + padding,
|
offsetX: anchorWidth / 2 + padding,
|
||||||
visible: resizeEnabled && enabledAnchors.indexOf('middle-left') >= 0,
|
visible: resizeEnabled && enabledAnchors.indexOf('middle-left') >= 0,
|
||||||
});
|
});
|
||||||
this._batchChangeChild('.middle-right', {
|
this._batchChangeChild('.middle-right', {
|
||||||
x: width,
|
x: width,
|
||||||
y: height / 2,
|
y: height / 2,
|
||||||
offsetX: anchorSize / 2 - padding,
|
offsetX: anchorWidth / 2 - padding,
|
||||||
visible: resizeEnabled && enabledAnchors.indexOf('middle-right') >= 0,
|
visible: resizeEnabled && enabledAnchors.indexOf('middle-right') >= 0,
|
||||||
});
|
});
|
||||||
this._batchChangeChild('.bottom-left', {
|
this._batchChangeChild('.bottom-left', {
|
||||||
x: 0,
|
x: 0,
|
||||||
y: height,
|
y: height,
|
||||||
offsetX: anchorSize / 2 + padding,
|
offsetX: anchorWidth / 2 + padding,
|
||||||
offsetY: anchorSize / 2 - padding,
|
offsetY: anchorHeight / 2 - padding,
|
||||||
visible: resizeEnabled && enabledAnchors.indexOf('bottom-left') >= 0,
|
visible: resizeEnabled && enabledAnchors.indexOf('bottom-left') >= 0,
|
||||||
});
|
});
|
||||||
this._batchChangeChild('.bottom-center', {
|
this._batchChangeChild('.bottom-center', {
|
||||||
x: width / 2,
|
x: width / 2,
|
||||||
y: height,
|
y: height,
|
||||||
offsetY: anchorSize / 2 - padding,
|
offsetY: anchorHeight / 2 - padding,
|
||||||
visible: resizeEnabled && enabledAnchors.indexOf('bottom-center') >= 0,
|
visible: resizeEnabled && enabledAnchors.indexOf('bottom-center') >= 0,
|
||||||
});
|
});
|
||||||
this._batchChangeChild('.bottom-right', {
|
this._batchChangeChild('.bottom-right', {
|
||||||
x: width,
|
x: width,
|
||||||
y: height,
|
y: height,
|
||||||
offsetX: anchorSize / 2 - padding,
|
offsetX: anchorWidth / 2 - padding,
|
||||||
offsetY: anchorSize / 2 - padding,
|
offsetY: anchorHeight / 2 - padding,
|
||||||
visible: resizeEnabled && enabledAnchors.indexOf('bottom-right') >= 0,
|
visible: resizeEnabled && enabledAnchors.indexOf('bottom-right') >= 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -1263,7 +1265,7 @@ export class Transformer extends Group {
|
|||||||
nodes: GetSet<Node[], this>;
|
nodes: GetSet<Node[], this>;
|
||||||
enabledAnchors: GetSet<string[], this>;
|
enabledAnchors: GetSet<string[], this>;
|
||||||
rotationSnaps: GetSet<number[], this>;
|
rotationSnaps: GetSet<number[], this>;
|
||||||
anchorSize: GetSet<number, this>;
|
anchorSize: GetSet<number | number[], this>;
|
||||||
resizeEnabled: GetSet<boolean, this>;
|
resizeEnabled: GetSet<boolean, this>;
|
||||||
rotateEnabled: GetSet<boolean, this>;
|
rotateEnabled: GetSet<boolean, this>;
|
||||||
rotateAnchorOffset: GetSet<number, this>;
|
rotateAnchorOffset: GetSet<number, this>;
|
||||||
@ -1366,20 +1368,24 @@ Factory.addGetterSetter(
|
|||||||
* transformer.resizeEnabled(false);
|
* transformer.resizeEnabled(false);
|
||||||
*/
|
*/
|
||||||
Factory.addGetterSetter(Transformer, 'resizeEnabled', true);
|
Factory.addGetterSetter(Transformer, 'resizeEnabled', true);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get/set anchor size. Default is 10
|
* get/set anchor size. Default is 10
|
||||||
* @name Konva.Transformer#anchorSize
|
* @name Konva.Transformer#anchorSize
|
||||||
* @method
|
* @method
|
||||||
* @param {Number} size
|
* @param {Number | Array<number>} size
|
||||||
* @returns {Number}
|
* @returns {Number | Arrya<number>}
|
||||||
* @example
|
* @example
|
||||||
* // get
|
* // get
|
||||||
* var anchorSize = transformer.anchorSize();
|
* var anchorSize = transformer.anchorSize();
|
||||||
*
|
*
|
||||||
* // set
|
* // set height and width equal
|
||||||
* transformer.anchorSize(20)
|
* transformer.anchorSize(20);
|
||||||
|
*
|
||||||
|
* // set height and width separatley, in order of width then height
|
||||||
|
* transformer.anchorSizr([10, 15])
|
||||||
*/
|
*/
|
||||||
Factory.addGetterSetter(Transformer, 'anchorSize', 10, getNumberValidator());
|
Factory.addGetterSetter(Transformer, 'anchorSize', 10, getNumberOrArrayOfNumbersValidator(2));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get/set ability to rotate.
|
* get/set ability to rotate.
|
||||||
|
@ -4782,4 +4782,33 @@ describe('Transformer', function () {
|
|||||||
tr.nodes([layer]);
|
tr.nodes([layer]);
|
||||||
assert.equal(tr.nodes().length, 0);
|
assert.equal(tr.nodes().length, 0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('set anchor width and height to different sizes with constructor using anchorSize', function () {
|
||||||
|
const stage = addStage();
|
||||||
|
|
||||||
|
const layer = new Konva.Layer();
|
||||||
|
stage.add(layer);
|
||||||
|
|
||||||
|
const tr = new Konva.Transformer({
|
||||||
|
anchorSize: [10, 15],
|
||||||
|
});
|
||||||
|
layer.add(tr);
|
||||||
|
console.log(tr.anchorSize())
|
||||||
|
|
||||||
|
assert.deepEqual(tr.anchorSize(), [10, 15]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('set anchor width and height to different sizes with setter', function () {
|
||||||
|
const stage = addStage();
|
||||||
|
|
||||||
|
const layer = new Konva.Layer();
|
||||||
|
stage.add(layer);
|
||||||
|
|
||||||
|
const tr = new Konva.Transformer();
|
||||||
|
layer.add(tr);
|
||||||
|
|
||||||
|
tr.anchorSize([12, 14]);
|
||||||
|
|
||||||
|
assert.deepEqual(tr.anchorSize(), [12, 14]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user