mirror of
https://github.com/konvajs/konva.git
synced 2025-10-15 12:34:52 +08:00
better capable konva context with native context
This commit is contained in:
54
test/unit/Context-test.js
Normal file
54
test/unit/Context-test.js
Normal file
@@ -0,0 +1,54 @@
|
||||
suite('Context', function() {
|
||||
// ======================================================
|
||||
var contextMethods = ['clearRect', 'fillRect', 'strokeRect',
|
||||
'fillText', 'strokeText', 'measureText', 'getLineDash', 'setLineDash',
|
||||
'createLinearGradient', 'createRadialGradient', 'createPattern',
|
||||
'beginPath', 'closePath', 'moveTo', 'lineTo', 'bezierCurveTo', 'quadraticCurveTo',
|
||||
'arc', 'arcTo', 'rect', 'fill', 'stroke', 'clip', 'isPointInPath',
|
||||
'scale', 'rotate', 'translate', 'transform', 'setTransform', 'drawImage',
|
||||
'createImageData', 'getImageData', 'putImageData', 'save', 'restore'];
|
||||
|
||||
var contextProperties = ['fillStyle', 'strokeStyle', 'shadowColor', 'shadowBlur', 'shadowOffsetX',
|
||||
'shadowOffsetY', 'lineCap', 'lineJoin', 'lineWidth', 'miterLimit', 'font', 'textAlign', 'textBaseline',
|
||||
'globalAlpha', 'globalCompositeOperation'];
|
||||
|
||||
test('context wrapper should work like native context', function() {
|
||||
var stage = addStage();
|
||||
|
||||
var layer = new Konva.Layer();
|
||||
|
||||
var circle = new Konva.Circle({
|
||||
x: 100,
|
||||
y: 70,
|
||||
radius: 70,
|
||||
fill: 'green',
|
||||
stroke: 'blue',
|
||||
strokeWidth: 4,
|
||||
draggable : true
|
||||
});
|
||||
|
||||
layer.add(circle);
|
||||
stage.add(layer);
|
||||
|
||||
var context = layer.getContext();
|
||||
var nativeContext = context._context;
|
||||
|
||||
contextMethods.forEach(function(method) {
|
||||
assert.equal(typeof nativeContext[method], 'function', 'native context has no method ' + method);
|
||||
assert.equal(typeof context[method], 'function', 'context wrapper has no method ' + method);
|
||||
});
|
||||
|
||||
contextProperties.forEach(function(prop) {
|
||||
assert.equal(nativeContext.hasOwnProperty(prop), true, 'native context has no property ' + prop);
|
||||
assert.equal(context[prop] !== undefined, true, 'context wrapper has no property ' + prop);
|
||||
});
|
||||
|
||||
// test get
|
||||
nativeContext.fillStyle = '#ff0000';
|
||||
assert.equal(context.fillStyle, '#ff0000');
|
||||
|
||||
// test set
|
||||
context.globalAlpha = 0.5;
|
||||
assert.equal(context.globalAlpha, 0.5);
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user