Skip to content

Commit eb86ba6

Browse files
committed
perf(): optimize rect rendering + calcOwnMatrix
1 parent 81bc042 commit eb86ba6

File tree

1 file changed

+103
-1
lines changed

1 file changed

+103
-1
lines changed

src/scripts/fabric.js

+103-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,107 @@
11
import * as fabric from "fabric";
22
import Engine from "./engine";
33

4+
// fabric.StaticCanvas.prototype.renderCanvas = function (ctx, objects) {
5+
// this.clearContext(this.getContext());
6+
// this._renderObjects(ctx, objects);
7+
// };
8+
9+
const uAffine = (product, curr) =>
10+
curr ? fabric.util.multiplyTransformMatrices(curr, product, false) : product;
11+
12+
const u = (product, curr) =>
13+
curr ? fabric.util.multiplyTransformMatrices(curr, product, true) : product;
14+
15+
const multiplyTransformMatrixArray = (matrices, is2x2) =>
16+
matrices.reduceRight(is2x2 ? u : uAffine, fabric.iMatrix.concat());
17+
18+
class OptimizedRect extends fabric.Rect {
19+
m;
20+
calcOwnMatrix() {
21+
// return [1, 0, 0, 1, this.left, this.top];
22+
if (this.m) {
23+
return this.m;
24+
}
25+
const {
26+
angle,
27+
scaleX,
28+
scaleY,
29+
flipX,
30+
flipY,
31+
skewX,
32+
skewY,
33+
left: x,
34+
top: y,
35+
} = this;
36+
37+
const m = multiplyTransformMatrixArray(
38+
[
39+
// fabric.util.createTranslateMatrix(x, y),
40+
angle && fabric.util.createRotateMatrix({ angle }),
41+
(scaleX !== 1 || scaleY !== 1 || flipX || flipY) &&
42+
fabric.util.createScaleMatrix(
43+
flipX ? -scaleX : scaleX,
44+
flipY ? -scaleY : scaleY
45+
),
46+
skewX && fabric.util.createSkewXMatrix(skewX),
47+
skewY && fabric.util.createSkewYMatrix(skewY),
48+
],
49+
false
50+
);
51+
52+
m[4] = x;
53+
m[5] = y;
54+
return (this.m = m);
55+
56+
// return fabric.util.composeMatrix({
57+
// angle: this.angle,
58+
// translateX: center.x,
59+
// translateY: center.y,
60+
// scaleX: this.scaleX,
61+
// scaleY: this.scaleY,
62+
// skewX: this.skewX,
63+
// skewY: this.skewY,
64+
// flipX: this.flipX,
65+
// flipY: this.flipY,
66+
// });
67+
}
68+
69+
/**
70+
*
71+
* @param {CanvasRenderingContext2D} ctx
72+
*/
73+
render(ctx) {
74+
const dpr = this.canvas.getRetinaScaling();
75+
// ctx.resetTransform();
76+
// ctx.scale(dpr, dpr);
77+
// this.transform(ctx);
78+
const t = this.calcTransformMatrix();
79+
ctx.setTransform(
80+
dpr * t[0],
81+
t[1],
82+
t[2],
83+
dpr * t[3],
84+
dpr * t[4],
85+
dpr * t[5]
86+
);
87+
ctx.strokeRect(-this.width / 2, -this.height / 2, this.width, this.height);
88+
ctx.fillStyle = "white";
89+
ctx.fillRect(-this.width / 2, -this.height / 2, this.width, this.height);
90+
// ctx.beginPath();
91+
// ctx.rect(-this.width / 2, -this.height / 2, this.width, this.height);
92+
// ctx.stroke();
93+
// ctx.fill();
94+
95+
// ctx.beginPath();
96+
// ctx.rect(-this.width / 2, -this.height / 2, this.width, this.height);
97+
// ctx.closePath();
98+
// ctx.fillStyle = "white";
99+
100+
// ctx.stroke();
101+
// ctx.fill();
102+
}
103+
}
104+
4105
class FabricEngine extends Engine {
5106
constructor() {
6107
super();
@@ -21,6 +122,7 @@ class FabricEngine extends Engine {
21122
const r = rects[i];
22123
r.x -= r.speed;
23124
r.el.left = r.x;
125+
delete r.el.m;
24126
if (r.x + r.size < 0) {
25127
r.x = this.width + r.size;
26128
}
@@ -44,7 +146,7 @@ class FabricEngine extends Engine {
44146
const size = 10 + Math.random() * 40;
45147
const speed = 1 + Math.random();
46148

47-
const fRect = new fabric.Rect({
149+
const fRect = new OptimizedRect({
48150
width: size,
49151
height: size,
50152
fill: "white",

0 commit comments

Comments
 (0)