mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 09:48:30 +08:00
clipping function
This commit is contained in:
parent
82b359bb08
commit
dbcc17041f
@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
|
|
||||||
## [Not released][Not released]
|
## [Not released][Not released]
|
||||||
|
|
||||||
|
## Added
|
||||||
|
- Custom clip function
|
||||||
|
|
||||||
## [0.14.0][2016-06-17]
|
## [0.14.0][2016-06-17]
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
2
konva.d.ts
vendored
2
konva.d.ts
vendored
@ -283,6 +283,8 @@ declare module Konva {
|
|||||||
clipX(clipX: number) : Container;
|
clipX(clipX: number) : Container;
|
||||||
clipY(): number;
|
clipY(): number;
|
||||||
clipY(clipY: number) : Container;
|
clipY(clipY: number) : Container;
|
||||||
|
clipFunct(): number;
|
||||||
|
clipFunct(clipFunc: Function) : Container;
|
||||||
destroyChildren() : void;
|
destroyChildren() : void;
|
||||||
find(selector? : string): Collection;
|
find(selector? : string): Collection;
|
||||||
getAllIntersections(pos: Vector2d): Node[];
|
getAllIntersections(pos: Vector2d): Node[];
|
||||||
|
@ -3,3 +3,4 @@
|
|||||||
* @param {Number} [config.clipY] set clip y
|
* @param {Number} [config.clipY] set clip y
|
||||||
* @param {Number} [config.clipWidth] set clip width
|
* @param {Number} [config.clipWidth] set clip width
|
||||||
* @param {Number} [config.clipHeight] set clip height
|
* @param {Number} [config.clipHeight] set clip height
|
||||||
|
* @param {Function} [config.clipFunc] set clip func
|
||||||
|
@ -368,17 +368,21 @@
|
|||||||
context = canvas && canvas.getContext(),
|
context = canvas && canvas.getContext(),
|
||||||
clipWidth = this.getClipWidth(),
|
clipWidth = this.getClipWidth(),
|
||||||
clipHeight = this.getClipHeight(),
|
clipHeight = this.getClipHeight(),
|
||||||
hasClip = clipWidth && clipHeight,
|
clipFunc = this.getClipFunc(),
|
||||||
|
hasClip = clipWidth && clipHeight || clipFunc,
|
||||||
clipX, clipY;
|
clipX, clipY;
|
||||||
|
|
||||||
if (hasClip && layer) {
|
if (hasClip && layer) {
|
||||||
clipX = this.getClipX();
|
|
||||||
clipY = this.getClipY();
|
|
||||||
|
|
||||||
context.save();
|
context.save();
|
||||||
layer._applyTransform(this, context);
|
layer._applyTransform(this, context);
|
||||||
context.beginPath();
|
context.beginPath();
|
||||||
context.rect(clipX, clipY, clipWidth, clipHeight);
|
if (clipFunc) {
|
||||||
|
clipFunc.call(this, context, this);
|
||||||
|
} else {
|
||||||
|
clipX = this.getClipX();
|
||||||
|
clipY = this.getClipY();
|
||||||
|
context.rect(clipX, clipY, clipWidth, clipHeight);
|
||||||
|
}
|
||||||
context.clip();
|
context.clip();
|
||||||
context.reset();
|
context.reset();
|
||||||
}
|
}
|
||||||
@ -539,5 +543,23 @@
|
|||||||
* container.clipHeight(100);
|
* container.clipHeight(100);
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
Konva.Factory.addGetterSetter(Konva.Container, 'clipFunc');
|
||||||
|
/**
|
||||||
|
* get/set clip function
|
||||||
|
* @name clipFunc
|
||||||
|
* @method
|
||||||
|
* @memberof Konva.Container.prototype
|
||||||
|
* @param {Function} function
|
||||||
|
* @returns {Function}
|
||||||
|
* @example
|
||||||
|
* // get clip function
|
||||||
|
* var clipFunction = container.clipFunc();
|
||||||
|
*
|
||||||
|
* // set clip height
|
||||||
|
* container.clipFunc(function(ctx) {
|
||||||
|
* ctx.rect(0, 0, 100, 100);
|
||||||
|
* });
|
||||||
|
*/
|
||||||
|
|
||||||
Konva.Collection.mapMethods(Konva.Container);
|
Konva.Collection.mapMethods(Konva.Container);
|
||||||
})();
|
})();
|
||||||
|
@ -24,6 +24,42 @@ suite('Container', function() {
|
|||||||
layer.draw();
|
layer.draw();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ======================================================
|
||||||
|
test.only('clip function', function() {
|
||||||
|
var stage = addStage();
|
||||||
|
|
||||||
|
// cliped by circle is the same as draw circle
|
||||||
|
var layer = new Konva.Layer();
|
||||||
|
stage.add(layer);
|
||||||
|
var circle = new Konva.Circle({
|
||||||
|
fill: 'green',
|
||||||
|
x: 50, y: 50,
|
||||||
|
radius: 40
|
||||||
|
});
|
||||||
|
layer.add(circle);
|
||||||
|
|
||||||
|
|
||||||
|
layer.draw();
|
||||||
|
|
||||||
|
var clipedLayer = new Konva.Layer({
|
||||||
|
clipFunc: function(ctx) {
|
||||||
|
ctx.arc(50, 50, 40, 0, Math.PI * 2, false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
stage.add(clipedLayer);
|
||||||
|
var rect = new Konva.Rect({
|
||||||
|
x: 10,
|
||||||
|
y: 10,
|
||||||
|
fill: 'green',
|
||||||
|
width: 200,
|
||||||
|
height: 200
|
||||||
|
});
|
||||||
|
clipedLayer.add(rect);
|
||||||
|
stage.draw();
|
||||||
|
|
||||||
|
compareLayers(layer, clipedLayer, 150);
|
||||||
|
});
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
test('adder validation', function() {
|
test('adder validation', function() {
|
||||||
var stage = addStage();
|
var stage = addStage();
|
||||||
|
Loading…
Reference in New Issue
Block a user