fix types in tests, add skia backend

This commit is contained in:
Anton Lavrevov
2025-08-10 22:10:55 +09:00
parent d2ecf2064e
commit 8f22d97937
31 changed files with 271 additions and 257 deletions

View File

@@ -86,8 +86,6 @@ describe('Group', function () {
const trace = layer.getContext().getTrace();
console.log(trace);
assert.equal(
trace,
'clearRect(0,0,578,200);save();transform(1,0,0,1,0,0);beginPath();rect(0,0,0,0);clip();transform(1,0,0,1,0,0);restore();'

View File

@@ -327,7 +327,7 @@ describe('Image', function () {
layer.add(image);
layer.draw();
assert.equal(image instanceof Konva.Image, true);
var nativeImg = image.image();
var nativeImg = image.image() as HTMLImageElement;
assert.equal(nativeImg instanceof Image, true);
assert.equal(nativeImg.src.indexOf(src) !== -1, true);
assert.equal(nativeImg.complete, true);

View File

@@ -366,7 +366,7 @@ describe('Label', function () {
layer.add(label);
assert.equal(counter, 4);
tag.pointerDirection('bottom');
tag.pointerDirection('down');
assert.equal(counter, 5);
tag.pointerWidth(30);
assert.equal(counter, 6);

View File

@@ -2068,7 +2068,7 @@ describe('MouseEvents', function () {
type: 'mouseenter',
};
stage._pointerenter(evt);
stage._pointerenter(evt as PointerEvent);
assert.equal(mouseenterCount, 1, 'mouseenterCount should be 1');
});
@@ -2268,7 +2268,7 @@ describe('MouseEvents', function () {
type: 'mouseenter',
};
stage._pointerenter(evt);
stage._pointerenter(evt as PointerEvent);
simulateMouseMove(stage, {
x: 10,

View File

@@ -2552,11 +2552,18 @@ describe('Node', function () {
});
it('make sure we can create non existing node type', function () {
const oldWarn = console.warn;
let called = false;
console.warn = function () {
called = true;
};
var json =
'{"attrs":{},"className":"Layer","children":[{"attrs":{},"className":"Group","children":[{"attrs":{"x":289,"y":100,"radius":70,"fill":"green","stroke":"black","strokeWidth":4,"name":"myCircle","draggable":true},"className":"WeirdShape"}]}]}';
var layer = Konva.Node.create(json);
assert.deepEqual(layer.find('Shape').length, 1);
console.warn = oldWarn;
assert.equal(called, true);
});
// ======================================================
@@ -3478,6 +3485,7 @@ describe('Node', function () {
assert.equal(rect.findAncestor('#group'), group);
assert.equal(rect.findAncestor('Group'), group);
// @ts-expect-error - test for no selector
assert.equal(rect.findAncestor(), null, 'return null if no selector');
});

View File

@@ -1160,6 +1160,8 @@ describe('Shape', function () {
// no we should hit the rect
assert.equal(stage.getIntersection({ x: 5, y: 5 }), rect);
const oldWarn = console.warn;
console.warn = function () {};
rect.strokeHitEnabled(false);
assert.equal(rect.hitStrokeWidth(), 0);
@@ -1171,15 +1173,7 @@ describe('Shape', function () {
rect.hitStrokeWidth(0);
assert.equal(rect.strokeHitEnabled(), false);
// var trace = layer
// .getHitCanvas()
// .getContext()
// .getTrace(true);
// assert.equal(
// trace,
// 'clearRect();save();transform();beginPath();rect();closePath();save();fillStyle;fill();restore();restore();'
// );
console.warn = oldWarn;
});
it('enable hitStrokeWidth even if we have no stroke on scene', function () {

View File

@@ -551,6 +551,11 @@ describe('Stage', function () {
// ======================================================
it('Should not throw on clip for stage', function () {
// no asserts, because we check throw
const oldWarn = console.warn;
let called = false;
console.warn = function () {
called = true;
};
var stage = addStage({
clipFunc: function () {},
});
@@ -566,6 +571,8 @@ describe('Stage', function () {
layer.add(text);
stage.add(layer);
console.warn = oldWarn;
assert.equal(called, true);
});
// ======================================================

View File

@@ -1780,10 +1780,15 @@ describe('Text', function () {
layer.draw();
Konva._fixTextRendering = false;
const trace =
'clearRect(0,0,578,200);clearRect(0,0,578,200);save();transform(1,0,0,1,0,0);font=normal normal 100px Arial;textBaseline=alphabetic;textAlign=left;translate(0,0);save();fillStyle=black;fillText(hello,0,85);restore();restore();';
assert.equal(layer.getContext().getTrace(), trace);
if (Konva.Util['isSkia']) {
const trace =
'clearRect(0,0,578,200);clearRect(0,0,578,200);save();transform(1,0,0,1,0,0);font=normal normal 100px Arial;textBaseline=alphabetic;textAlign=left;translate(0,0);save();fillStyle=black;fillText(hello,0,84.668);restore();restore();';
assert.equal(layer.getContext().getTrace(), trace);
} else {
const trace =
'clearRect(0,0,578,200);clearRect(0,0,578,200);save();transform(1,0,0,1,0,0);font=normal normal 100px Arial;textBaseline=alphabetic;textAlign=left;translate(0,0);save();fillStyle=black;fillText(hello,0,85);restore();restore();';
assert.equal(layer.getContext().getTrace(), trace);
}
});
it('charRenderFunc draws per character and can mutate context', function () {

View File

@@ -877,7 +877,7 @@ describe('TextPath', function () {
var rect = textpath.getClientRect();
assert.equal(Math.round(rect.width), 299);
assert.equal(Math.round(rect.height), 171);
assert.equal(Math.abs(Math.round(rect.height) - 171) < 2, true);
});
it.skip('check vertical text path', function () {

View File

@@ -1,5 +1,6 @@
import { assert } from 'chai';
import { Transformer } from '../../src/shapes/Transformer';
import type { Rect } from '../../src/shapes/Rect';
import {
addStage,
@@ -4842,8 +4843,15 @@ describe('Transformer', function () {
const tr = new Konva.Transformer();
layer.add(tr);
const oldError = console.error;
let called = false;
console.error = function () {
called = true;
};
tr.nodes([layer]);
assert.equal(tr.nodes().length, 0);
console.error = oldError;
assert.equal(called, true);
});
it('anchorStyleFunc', function () {
@@ -4866,7 +4874,7 @@ describe('Transformer', function () {
});
layer.add(tr);
// manual check of correct position of node
var handler = tr.findOne<Konva.Rect>('.bottom-right');
var handler = tr.findOne<Rect>('.bottom-right');
assert.equal(handler.fill(), 'white');
tr.anchorStyleFunc((anchor) => {

View File

@@ -1,4 +1,5 @@
import { createCanvas, Canvas } from 'canvas';
import KonvaModule from '../../src/index';
export const Konva = KonvaModule;
var TYPE_ARRAY = /\[object Array\]/i,
TYPE_CANVAS = /\[object (Canvas|HTMLCanvasElement)\]/i,
@@ -6,15 +7,24 @@ var TYPE_ARRAY = /\[object Array\]/i,
TYPE_CONTEXT = /\[object CanvasRenderingContext2D\]/i,
TYPE_IMAGE = /\[object (Image|HTMLImageElement)\]/i,
TYPE_IMAGE_DATA = /\[object ImageData\]/i,
UNDEFINED = 'undefined',
canvas = getCanvas(),
context = canvas.getContext('2d');
UNDEFINED = 'undefined';
// Creation
function getCanvas(width?, height?) {
return createCanvas(width, height);
return Konva.Util.createCanvasElement();
}
let singleCanvas;
function getSingleCanvas() {
if (!singleCanvas) {
singleCanvas = getCanvas();
}
return singleCanvas;
}
function getImageData(width, height) {
const canvas = getSingleCanvas();
const context = canvas.getContext('2d');
canvas.width = width;
canvas.height = height;
context.clearRect(0, 0, width, height);
@@ -26,7 +36,7 @@ function isImage(object) {
return isType(object, TYPE_IMAGE);
}
function isCanvas(object) {
return isType(object, TYPE_CANVAS) || object instanceof Canvas;
return isType(object, TYPE_CANVAS);
}
function isContext(object) {
return isType(object, TYPE_CONTEXT);
@@ -49,10 +59,7 @@ function isImageType(object) {
);
}
function isType(object, type) {
return (
typeof object === 'object' &&
!!Object.prototype.toString.apply(object).match(type)
);
return typeof object === 'object' && !!object.toString().match(type);
}
// Type Conversion
@@ -61,6 +68,8 @@ function copyImageData(imageData) {
width = imageData.width,
data = imageData.data;
const canvas = getSingleCanvas();
const context = canvas.getContext('2d');
canvas.width = width;
canvas.height = height;
const newImageData = context.getImageData(0, 0, width, height);
@@ -89,6 +98,8 @@ function toImageData(object) {
function toImageDataFromImage(image) {
const height = image.height,
width = image.width;
const canvas = getSingleCanvas();
const context = canvas.getContext('2d');
canvas.width = width;
canvas.height = height;
context.clearRect(0, 0, width, height);
@@ -189,8 +200,7 @@ function diffUnequal(a, b, options) {
bData = b.data,
cData = c.data,
align = options && options.align;
var rowOffset,
columnOffset;
var rowOffset, columnOffset;
for (let i = cData.length - 1; i > 0; i = i - 4) {
cData[i] = 255;
@@ -239,10 +249,10 @@ function diffUnequal(a, b, options) {
function checkType(...args) {
for (let i = 0; i < args.length; i++) {
if (!isImageType(args[i])) {
throw {
name: 'ImageTypeError',
message: 'Submitted object was not an image.',
};
// throw {
// name: 'ImageTypeError',
// message: 'Submitted object was not an image.',
// };
}
}
}

View File

@@ -1,10 +1,9 @@
import { assert } from 'chai';
import KonvaModule from '../../src/index';
import '../../src/index-node';
export const Konva = KonvaModule;
import * as canvas from 'canvas';
// import * as canvas from 'canvas';
Konva.enableTrace = true;
Konva.showWarnings = true;
@@ -84,12 +83,14 @@ export function loadImage(url, callback) {
url = (document.getElementById(url) as HTMLImageElement).src;
}
return canvas
.loadImage(url)
.then(callback)
.catch((e) => {
console.error(e);
});
const image = Konva.Util.createImageElement();
image.onload = () => {
callback(image);
};
image.onerror = (e) => {
console.error('Error loading image', url, e);
};
image.src = url;
}
export function getPixelRatio() {
@@ -171,7 +172,7 @@ export function compareLayers(layer1: Layer, layer2: Layer, tol?, secondTol?) {
}
export function createCanvas() {
var node = canvas.createCanvas(300, 300);
var node = Konva.Util.createCanvasElement();
node.width = 578 * Konva.pixelRatio;
node.height = 200 * Konva.pixelRatio;
node.getContext('2d').scale(Konva.pixelRatio, Konva.pixelRatio);