konva/src/Context.ts

854 lines
19 KiB
TypeScript
Raw Normal View History

2019-02-23 01:46:46 +08:00
import { Util } from './Util';
2019-03-07 11:19:32 +08:00
import { Konva } from './Global';
2019-01-02 04:59:27 +08:00
import { Canvas } from './Canvas';
import { Shape } from './Shape';
2019-01-02 04:59:27 +08:00
var COMMA = ',',
OPEN_PAREN = '(',
CLOSE_PAREN = ')',
OPEN_PAREN_BRACKET = '([',
CLOSE_BRACKET_PAREN = '])',
SEMICOLON = ';',
DOUBLE_PAREN = '()',
// EMPTY_STRING = '',
EQUALS = '=',
// SET = 'set',
CONTEXT_METHODS = [
'arc',
'arcTo',
'beginPath',
'bezierCurveTo',
'clearRect',
'clip',
'closePath',
'createLinearGradient',
'createPattern',
'createRadialGradient',
'drawImage',
2019-05-10 06:09:05 +08:00
'ellipse',
2019-01-02 04:59:27 +08:00
'fill',
'fillText',
'getImageData',
'createImageData',
'lineTo',
'moveTo',
'putImageData',
'quadraticCurveTo',
'rect',
'restore',
'rotate',
'save',
'scale',
'setLineDash',
'setTransform',
'stroke',
'strokeText',
'transform',
'translate',
2019-01-02 04:59:27 +08:00
];
var CONTEXT_PROPERTIES = [
'fillStyle',
'strokeStyle',
'shadowColor',
'shadowBlur',
'shadowOffsetX',
'shadowOffsetY',
'lineCap',
'lineDashOffset',
'lineJoin',
'lineWidth',
'miterLimit',
'font',
'textAlign',
'textBaseline',
'globalAlpha',
'globalCompositeOperation',
'imageSmoothingEnabled',
2019-01-02 04:59:27 +08:00
];
2019-02-24 09:54:20 +08:00
const traceArrMax = 100;
2019-01-02 04:59:27 +08:00
/**
2019-01-06 16:01:20 +08:00
* Konva wrapper around native 2d canvas context. It has almost the same API of 2d context with some additional functions.
2019-08-05 14:54:08 +08:00
* With core Konva shapes you don't need to use this object. But you will use it if you want to create
* a [custom shape](/docs/react/Custom_Shape.html) or a [custom hit regions](/docs/events/Custom_Hit_Region.html).
* For full information about each 2d context API use [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D)
2019-01-02 04:59:27 +08:00
* @constructor
* @memberof Konva
2019-01-06 16:01:20 +08:00
* @example
* const rect = new Konva.Shape({
* fill: 'red',
* width: 100,
* height: 100,
* sceneFunc: (ctx, shape) => {
* // ctx - is context wrapper
* // shape - is instance of Konva.Shape, so it equals to "rect" variable
* ctx.rect(0, 0, shape.getAttr('width'), shape.getAttr('height'));
*
* // automatically fill shape from props and draw hit region
* ctx.fillStrokeShape(shape);
* }
* })
2019-01-02 04:59:27 +08:00
*/
export class Context {
canvas: Canvas;
_context: CanvasRenderingContext2D;
traceArr: Array<String>;
2019-02-23 01:46:46 +08:00
constructor(canvas: Canvas) {
2019-01-02 04:59:27 +08:00
this.canvas = canvas;
2019-02-23 01:46:46 +08:00
this._context = canvas._canvas.getContext('2d') as CanvasRenderingContext2D;
2019-01-02 04:59:27 +08:00
2019-03-07 11:19:32 +08:00
if (Konva.enableTrace) {
2019-01-02 04:59:27 +08:00
this.traceArr = [];
this._enableTrace();
}
}
2019-02-23 01:46:46 +08:00
2019-01-02 04:59:27 +08:00
/**
* fill shape
* @method
2019-01-06 16:01:20 +08:00
* @name Konva.Context#fillShape
2019-01-02 04:59:27 +08:00
* @param {Konva.Shape} shape
*/
fillShape(shape: Shape) {
if (shape.fillEnabled()) {
2019-01-02 04:59:27 +08:00
this._fill(shape);
}
}
_fill(shape) {
// abstract
}
/**
* stroke shape
* @method
2019-01-06 16:01:20 +08:00
* @name Konva.Context#strokeShape
2019-01-02 04:59:27 +08:00
* @param {Konva.Shape} shape
*/
strokeShape(shape: Shape) {
if (shape.hasStroke()) {
2019-01-02 04:59:27 +08:00
this._stroke(shape);
}
}
_stroke(shape) {
// abstract
}
/**
* fill then stroke
* @method
2019-01-06 16:01:20 +08:00
* @name Konva.Context#fillStrokeShape
2019-01-02 04:59:27 +08:00
* @param {Konva.Shape} shape
*/
fillStrokeShape(shape: Shape) {
this.fillShape(shape);
this.strokeShape(shape);
2019-01-02 04:59:27 +08:00
}
2019-01-06 16:01:20 +08:00
2019-01-02 04:59:27 +08:00
getTrace(relaxed) {
var traceArr = this.traceArr,
len = traceArr.length,
str = '',
n,
trace,
method,
args;
for (n = 0; n < len; n++) {
trace = traceArr[n];
method = trace.method;
// methods
if (method) {
args = trace.args;
str += method;
if (relaxed) {
str += DOUBLE_PAREN;
} else {
if (Util._isArray(args[0])) {
str += OPEN_PAREN_BRACKET + args.join(COMMA) + CLOSE_BRACKET_PAREN;
} else {
str += OPEN_PAREN + args.join(COMMA) + CLOSE_PAREN;
}
}
} else {
// properties
str += trace.property;
if (!relaxed) {
str += EQUALS + trace.val;
}
}
str += SEMICOLON;
}
return str;
}
2019-01-06 16:01:20 +08:00
2019-01-02 04:59:27 +08:00
clearTrace() {
this.traceArr = [];
}
_trace(str) {
var traceArr = this.traceArr,
len;
traceArr.push(str);
len = traceArr.length;
2019-02-24 09:54:20 +08:00
if (len >= traceArrMax) {
2019-01-02 04:59:27 +08:00
traceArr.shift();
}
}
/**
* reset canvas context transform
* @method
2019-01-06 16:01:20 +08:00
* @name Konva.Context#reset
2019-01-02 04:59:27 +08:00
*/
reset() {
var pixelRatio = this.getCanvas().getPixelRatio();
this.setTransform(1 * pixelRatio, 0, 0, 1 * pixelRatio, 0, 0);
}
/**
2019-01-06 16:01:20 +08:00
* get canvas wrapper
2019-01-02 04:59:27 +08:00
* @method
2019-01-06 16:01:20 +08:00
* @name Konva.Context#getCanvas
2019-01-02 04:59:27 +08:00
* @returns {Konva.Canvas}
*/
getCanvas() {
return this.canvas;
}
/**
* clear canvas
* @method
2019-01-06 16:01:20 +08:00
* @name Konva.Context#clear
2019-01-02 04:59:27 +08:00
* @param {Object} [bounds]
* @param {Number} [bounds.x]
* @param {Number} [bounds.y]
* @param {Number} [bounds.width]
* @param {Number} [bounds.height]
*/
clear(bounds?) {
var canvas = this.getCanvas();
if (bounds) {
this.clearRect(
bounds.x || 0,
bounds.y || 0,
bounds.width || 0,
bounds.height || 0
);
} else {
this.clearRect(
0,
0,
canvas.getWidth() / canvas.pixelRatio,
canvas.getHeight() / canvas.pixelRatio
);
}
}
_applyLineCap(shape) {
var lineCap = shape.getLineCap();
if (lineCap) {
this.setAttr('lineCap', lineCap);
}
}
_applyOpacity(shape) {
var absOpacity = shape.getAbsoluteOpacity();
if (absOpacity !== 1) {
this.setAttr('globalAlpha', absOpacity);
}
}
_applyLineJoin(shape: Shape) {
2020-06-24 06:22:28 +08:00
var lineJoin = shape.attrs.lineJoin;
2019-01-02 04:59:27 +08:00
if (lineJoin) {
this.setAttr('lineJoin', lineJoin);
}
}
2019-01-06 16:01:20 +08:00
2019-01-02 04:59:27 +08:00
setAttr(attr, val) {
this._context[attr] = val;
}
2019-08-05 14:54:08 +08:00
/**
* arc function.
* @method
* @name Konva.Context#arc
*/
2019-01-02 04:59:27 +08:00
arc(a0, a1, a2, a3, a4, a5) {
this._context.arc(a0, a1, a2, a3, a4, a5);
}
2019-08-05 14:54:08 +08:00
/**
* arcTo function.
* @method
* @name Konva.Context#arcTo
*/
2019-09-09 23:27:30 +08:00
arcTo(a0, a1, a2, a3, a4) {
this._context.arcTo(a0, a1, a2, a3, a4);
2019-01-02 04:59:27 +08:00
}
2019-08-05 14:54:08 +08:00
/**
* beginPath function.
* @method
* @name Konva.Context#beginPath
*/
2019-01-02 04:59:27 +08:00
beginPath() {
this._context.beginPath();
}
2019-08-05 14:54:08 +08:00
/**
* bezierCurveTo function.
* @method
* @name Konva.Context#bezierCurveTo
*/
2019-01-02 04:59:27 +08:00
bezierCurveTo(a0, a1, a2, a3, a4, a5) {
this._context.bezierCurveTo(a0, a1, a2, a3, a4, a5);
}
2019-08-05 14:54:08 +08:00
/**
* clearRect function.
* @method
* @name Konva.Context#clearRect
*/
2019-01-02 04:59:27 +08:00
clearRect(a0, a1, a2, a3) {
this._context.clearRect(a0, a1, a2, a3);
}
2019-08-05 14:54:08 +08:00
/**
* clip function.
* @method
* @name Konva.Context#clip
*/
2019-01-02 04:59:27 +08:00
clip() {
this._context.clip();
}
2019-08-05 14:54:08 +08:00
/**
* closePath function.
* @method
* @name Konva.Context#closePath
*/
2019-01-02 04:59:27 +08:00
closePath() {
this._context.closePath();
}
2019-08-05 14:54:08 +08:00
/**
* createImageData function.
* @method
* @name Konva.Context#createImageData
*/
2019-01-02 04:59:27 +08:00
createImageData(a0, a1) {
var a = arguments;
if (a.length === 2) {
return this._context.createImageData(a0, a1);
} else if (a.length === 1) {
return this._context.createImageData(a0);
}
}
2019-08-05 14:54:08 +08:00
/**
* createLinearGradient function.
* @method
* @name Konva.Context#createLinearGradient
*/
2019-01-02 04:59:27 +08:00
createLinearGradient(a0, a1, a2, a3) {
return this._context.createLinearGradient(a0, a1, a2, a3);
}
2019-08-05 14:54:08 +08:00
/**
* createPattern function.
* @method
* @name Konva.Context#createPattern
*/
2019-01-02 04:59:27 +08:00
createPattern(a0, a1) {
return this._context.createPattern(a0, a1);
}
2019-08-05 14:54:08 +08:00
/**
* createRadialGradient function.
* @method
* @name Konva.Context#createRadialGradient
*/
2019-01-02 04:59:27 +08:00
createRadialGradient(a0, a1, a2, a3, a4, a5) {
return this._context.createRadialGradient(a0, a1, a2, a3, a4, a5);
}
2019-08-05 14:54:08 +08:00
/**
* drawImage function.
* @method
* @name Konva.Context#drawImage
*/
2019-05-11 20:56:55 +08:00
drawImage(a0, a1, a2, a3?, a4?, a5?, a6?, a7?, a8?) {
2019-01-02 04:59:27 +08:00
var a = arguments,
_context = this._context;
if (a.length === 3) {
_context.drawImage(a0, a1, a2);
} else if (a.length === 5) {
_context.drawImage(a0, a1, a2, a3, a4);
} else if (a.length === 9) {
_context.drawImage(a0, a1, a2, a3, a4, a5, a6, a7, a8);
}
}
2019-08-05 14:54:08 +08:00
/**
* ellipse function.
* @method
* @name Konva.Context#ellipse
*/
2019-05-10 06:09:05 +08:00
ellipse(a0, a1, a2, a3, a4, a5, a6, a7) {
this._context.ellipse(a0, a1, a2, a3, a4, a5, a6, a7);
}
2019-08-05 14:54:08 +08:00
/**
* isPointInPath function.
* @method
* @name Konva.Context#isPointInPath
*/
2019-01-02 04:59:27 +08:00
isPointInPath(x, y) {
return this._context.isPointInPath(x, y);
}
2019-08-05 14:54:08 +08:00
/**
* fill function.
* @method
* @name Konva.Context#fill
*/
2019-01-02 04:59:27 +08:00
fill() {
this._context.fill();
}
2019-08-05 14:54:08 +08:00
/**
* fillRect function.
* @method
* @name Konva.Context#fillRect
*/
2019-01-02 04:59:27 +08:00
fillRect(x, y, width, height) {
this._context.fillRect(x, y, width, height);
}
2019-08-05 14:54:08 +08:00
/**
* strokeRect function.
* @method
* @name Konva.Context#strokeRect
*/
2019-01-02 04:59:27 +08:00
strokeRect(x, y, width, height) {
this._context.strokeRect(x, y, width, height);
}
2019-08-05 14:54:08 +08:00
/**
* fillText function.
* @method
* @name Konva.Context#fillText
*/
2019-01-02 04:59:27 +08:00
fillText(a0, a1, a2) {
this._context.fillText(a0, a1, a2);
}
2019-08-05 14:54:08 +08:00
/**
* measureText function.
* @method
* @name Konva.Context#measureText
*/
2019-01-02 04:59:27 +08:00
measureText(text) {
return this._context.measureText(text);
}
2019-08-05 14:54:08 +08:00
/**
* getImageData function.
* @method
* @name Konva.Context#getImageData
*/
2019-01-02 04:59:27 +08:00
getImageData(a0, a1, a2, a3) {
return this._context.getImageData(a0, a1, a2, a3);
}
2019-08-05 14:54:08 +08:00
/**
* lineTo function.
* @method
* @name Konva.Context#lineTo
*/
2019-01-02 04:59:27 +08:00
lineTo(a0, a1) {
this._context.lineTo(a0, a1);
}
2019-08-05 14:54:08 +08:00
/**
* moveTo function.
* @method
* @name Konva.Context#moveTo
*/
2019-01-02 04:59:27 +08:00
moveTo(a0, a1) {
this._context.moveTo(a0, a1);
}
2019-08-05 14:54:08 +08:00
/**
* rect function.
* @method
* @name Konva.Context#rect
*/
2019-01-02 04:59:27 +08:00
rect(a0, a1, a2, a3) {
this._context.rect(a0, a1, a2, a3);
}
2019-08-05 14:54:08 +08:00
/**
* putImageData function.
* @method
* @name Konva.Context#putImageData
*/
2019-01-02 04:59:27 +08:00
putImageData(a0, a1, a2) {
this._context.putImageData(a0, a1, a2);
}
2019-08-05 14:54:08 +08:00
/**
* quadraticCurveTo function.
* @method
* @name Konva.Context#quadraticCurveTo
*/
2019-01-02 04:59:27 +08:00
quadraticCurveTo(a0, a1, a2, a3) {
this._context.quadraticCurveTo(a0, a1, a2, a3);
}
2019-08-05 14:54:08 +08:00
/**
* restore function.
* @method
* @name Konva.Context#restore
*/
2019-01-02 04:59:27 +08:00
restore() {
this._context.restore();
}
2019-08-05 14:54:08 +08:00
/**
* rotate function.
* @method
* @name Konva.Context#rotate
*/
2019-01-02 04:59:27 +08:00
rotate(a0) {
this._context.rotate(a0);
}
2019-08-05 14:54:08 +08:00
/**
* save function.
* @method
* @name Konva.Context#save
*/
2019-01-02 04:59:27 +08:00
save() {
this._context.save();
}
2019-08-05 14:54:08 +08:00
/**
* scale function.
* @method
* @name Konva.Context#scale
*/
2019-01-02 04:59:27 +08:00
scale(a0, a1) {
this._context.scale(a0, a1);
}
2019-08-05 14:54:08 +08:00
/**
* setLineDash function.
* @method
* @name Konva.Context#setLineDash
*/
2019-01-02 04:59:27 +08:00
setLineDash(a0) {
// works for Chrome and IE11
if (this._context.setLineDash) {
this._context.setLineDash(a0);
} else if ('mozDash' in this._context) {
// verified that this works in firefox
(<any>this._context['mozDash']) = a0;
} else if ('webkitLineDash' in this._context) {
// does not currently work for Safari
(<any>this._context['webkitLineDash']) = a0;
}
// no support for IE9 and IE10
}
2019-08-05 14:54:08 +08:00
/**
* getLineDash function.
* @method
* @name Konva.Context#getLineDash
*/
2019-01-02 04:59:27 +08:00
getLineDash() {
return this._context.getLineDash();
}
2019-08-05 14:54:08 +08:00
/**
* setTransform function.
* @method
* @name Konva.Context#setTransform
*/
2019-01-02 04:59:27 +08:00
setTransform(a0, a1, a2, a3, a4, a5) {
this._context.setTransform(a0, a1, a2, a3, a4, a5);
}
2019-08-05 14:54:08 +08:00
/**
* stroke function.
* @method
* @name Konva.Context#stroke
*/
2019-01-02 04:59:27 +08:00
stroke() {
this._context.stroke();
}
2019-08-05 14:54:08 +08:00
/**
* strokeText function.
* @method
* @name Konva.Context#strokeText
*/
2019-01-02 04:59:27 +08:00
strokeText(a0, a1, a2, a3) {
this._context.strokeText(a0, a1, a2, a3);
}
2019-08-05 14:54:08 +08:00
/**
* transform function.
* @method
* @name Konva.Context#transform
*/
2019-01-02 04:59:27 +08:00
transform(a0, a1, a2, a3, a4, a5) {
this._context.transform(a0, a1, a2, a3, a4, a5);
}
2019-08-05 14:54:08 +08:00
/**
* translate function.
* @method
* @name Konva.Context#translate
*/
2019-01-02 04:59:27 +08:00
translate(a0, a1) {
this._context.translate(a0, a1);
}
_enableTrace() {
var that = this,
len = CONTEXT_METHODS.length,
_simplifyArray = Util._simplifyArray,
origSetter = this.setAttr,
n,
args;
// to prevent creating scope function at each loop
var func = function (methodName) {
2019-01-02 04:59:27 +08:00
var origMethod = that[methodName],
ret;
that[methodName] = function () {
2019-01-02 04:59:27 +08:00
args = _simplifyArray(Array.prototype.slice.call(arguments, 0));
ret = origMethod.apply(that, arguments);
that._trace({
method: methodName,
args: args,
2019-01-02 04:59:27 +08:00
});
return ret;
};
};
// methods
for (n = 0; n < len; n++) {
func(CONTEXT_METHODS[n]);
}
// attrs
that.setAttr = function () {
2019-01-02 04:59:27 +08:00
origSetter.apply(that, arguments);
var prop = arguments[0];
var val = arguments[1];
if (
prop === 'shadowOffsetX' ||
prop === 'shadowOffsetY' ||
prop === 'shadowBlur'
) {
val = val / this.canvas.getPixelRatio();
}
that._trace({
property: prop,
val: val,
2019-01-02 04:59:27 +08:00
});
};
}
2019-02-19 01:12:03 +08:00
_applyGlobalCompositeOperation(node) {
var globalCompositeOperation = node.getGlobalCompositeOperation();
if (globalCompositeOperation !== 'source-over') {
this.setAttr('globalCompositeOperation', globalCompositeOperation);
}
}
2019-01-02 04:59:27 +08:00
}
CONTEXT_PROPERTIES.forEach(function (prop) {
2019-01-02 04:59:27 +08:00
Object.defineProperty(Context.prototype, prop, {
get() {
return this._context[prop];
},
set(val) {
this._context[prop] = val;
},
2019-01-02 04:59:27 +08:00
});
});
export class SceneContext extends Context {
_fillColor(shape) {
var fill = shape.fill();
this.setAttr('fillStyle', fill);
shape._fillFunc(this);
}
_fillPattern(shape) {
var fillPatternX = shape.getFillPatternX(),
fillPatternY = shape.getFillPatternY(),
2019-03-07 11:19:32 +08:00
fillPatternRotation = Konva.getAngle(shape.getFillPatternRotation()),
2019-01-25 13:20:15 +08:00
fillPatternOffsetX = shape.getFillPatternOffsetX(),
fillPatternOffsetY = shape.getFillPatternOffsetY(),
fillPatternScaleX = shape.getFillPatternScaleX(),
fillPatternScaleY = shape.getFillPatternScaleY();
2019-01-02 04:59:27 +08:00
if (fillPatternX || fillPatternY) {
this.translate(fillPatternX || 0, fillPatternY || 0);
}
2019-01-11 21:51:46 +08:00
2019-01-02 04:59:27 +08:00
if (fillPatternRotation) {
this.rotate(fillPatternRotation);
}
2019-01-11 21:51:46 +08:00
if (fillPatternScaleX || fillPatternScaleY) {
// this.scale(fillPatternScaleX, fillPatternScaleY);
}
2019-01-25 13:20:15 +08:00
if (fillPatternOffsetX || fillPatternOffsetY) {
this.translate(-1 * fillPatternOffsetX, -1 * fillPatternOffsetY);
2019-01-02 04:59:27 +08:00
}
2019-02-14 11:04:54 +08:00
this.setAttr('fillStyle', shape._getFillPattern());
2019-01-11 21:51:46 +08:00
shape._fillFunc(this);
2019-01-02 04:59:27 +08:00
}
_fillLinearGradient(shape) {
2019-02-14 11:04:54 +08:00
var grd = shape._getLinearGradient();
if (grd) {
2019-01-02 04:59:27 +08:00
this.setAttr('fillStyle', grd);
shape._fillFunc(this);
}
}
_fillRadialGradient(shape) {
2019-02-14 11:04:54 +08:00
var grd = shape._getRadialGradient();
if (grd) {
this.setAttr('fillStyle', grd);
shape._fillFunc(this);
2019-01-02 04:59:27 +08:00
}
}
_fill(shape) {
var hasColor = shape.fill(),
fillPriority = shape.getFillPriority();
// priority fills
if (hasColor && fillPriority === 'color') {
this._fillColor(shape);
return;
}
var hasPattern = shape.getFillPatternImage();
if (hasPattern && fillPriority === 'pattern') {
this._fillPattern(shape);
return;
}
var hasLinearGradient = shape.getFillLinearGradientColorStops();
if (hasLinearGradient && fillPriority === 'linear-gradient') {
this._fillLinearGradient(shape);
return;
}
var hasRadialGradient = shape.getFillRadialGradientColorStops();
if (hasRadialGradient && fillPriority === 'radial-gradient') {
this._fillRadialGradient(shape);
return;
}
// now just try and fill with whatever is available
if (hasColor) {
this._fillColor(shape);
} else if (hasPattern) {
this._fillPattern(shape);
} else if (hasLinearGradient) {
this._fillLinearGradient(shape);
} else if (hasRadialGradient) {
this._fillRadialGradient(shape);
}
}
_strokeLinearGradient(shape) {
var start = shape.getStrokeLinearGradientStartPoint(),
end = shape.getStrokeLinearGradientEndPoint(),
colorStops = shape.getStrokeLinearGradientColorStops(),
grd = this.createLinearGradient(start.x, start.y, end.x, end.y);
if (colorStops) {
// build color stops
for (var n = 0; n < colorStops.length; n += 2) {
grd.addColorStop(colorStops[n], colorStops[n + 1]);
}
this.setAttr('strokeStyle', grd);
}
}
_stroke(shape) {
var dash = shape.dash(),
// ignore strokeScaleEnabled for Text
strokeScaleEnabled = shape.getStrokeScaleEnabled();
if (shape.hasStroke()) {
if (!strokeScaleEnabled) {
this.save();
var pixelRatio = this.getCanvas().getPixelRatio();
this.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
}
this._applyLineCap(shape);
if (dash && shape.dashEnabled()) {
this.setLineDash(dash);
this.setAttr('lineDashOffset', shape.dashOffset());
}
this.setAttr('lineWidth', shape.strokeWidth());
if (!shape.getShadowForStrokeEnabled()) {
this.setAttr('shadowColor', 'rgba(0,0,0,0)');
}
var hasLinearGradient = shape.getStrokeLinearGradientColorStops();
if (hasLinearGradient) {
this._strokeLinearGradient(shape);
} else {
this.setAttr('strokeStyle', shape.stroke());
}
shape._strokeFunc(this);
if (!strokeScaleEnabled) {
this.restore();
}
}
}
_applyShadow(shape) {
var util = Util,
color = util.get(shape.getShadowRGBA(), 'black'),
blur = util.get(shape.getShadowBlur(), 5),
offset = util.get(shape.getShadowOffset(), {
x: 0,
y: 0,
2019-01-02 04:59:27 +08:00
}),
scale = shape.getAbsoluteScale(),
ratio = this.canvas.getPixelRatio(),
scaleX = scale.x * ratio,
scaleY = scale.y * ratio;
this.setAttr('shadowColor', color);
this.setAttr(
'shadowBlur',
blur * Math.min(Math.abs(scaleX), Math.abs(scaleY))
);
this.setAttr('shadowOffsetX', offset.x * scaleX);
this.setAttr('shadowOffsetY', offset.y * scaleY);
}
}
export class HitContext extends Context {
_fill(shape) {
this.save();
this.setAttr('fillStyle', shape.colorKey);
shape._fillFuncHit(this);
this.restore();
}
strokeShape(shape: Shape) {
if (shape.hasHitStroke()) {
this._stroke(shape);
}
}
2019-01-02 04:59:27 +08:00
_stroke(shape) {
if (shape.hasHitStroke()) {
2019-01-02 04:59:27 +08:00
// ignore strokeScaleEnabled for Text
var strokeScaleEnabled = shape.getStrokeScaleEnabled();
if (!strokeScaleEnabled) {
this.save();
var pixelRatio = this.getCanvas().getPixelRatio();
this.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
}
this._applyLineCap(shape);
2019-03-11 01:35:44 +08:00
var hitStrokeWidth = shape.hitStrokeWidth();
var strokeWidth =
hitStrokeWidth === 'auto' ? shape.strokeWidth() : hitStrokeWidth;
this.setAttr('lineWidth', strokeWidth);
2019-01-02 04:59:27 +08:00
this.setAttr('strokeStyle', shape.colorKey);
shape._strokeFuncHit(this);
if (!strokeScaleEnabled) {
this.restore();
}
}
}
}