Skip to content

Commit 2d18eae

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

File tree

1 file changed

+104
-1
lines changed

1 file changed

+104
-1
lines changed

src/scripts/fabric.js

+104-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,108 @@
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);
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 = fabric.util.multiplyTransformMatrixArray([
38+
angle && fabric.util.createRotateMatrix({ angle }),
39+
fabric.util.multiplyTransformMatrixArray(
40+
[
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+
true
50+
),
51+
]);
52+
53+
m[4] = x;
54+
m[5] = y;
55+
return (this.m = m);
56+
57+
// return fabric.util.composeMatrix({
58+
// angle: this.angle,
59+
// translateX: center.x,
60+
// translateY: center.y,
61+
// scaleX: this.scaleX,
62+
// scaleY: this.scaleY,
63+
// skewX: this.skewX,
64+
// skewY: this.skewY,
65+
// flipX: this.flipX,
66+
// flipY: this.flipY,
67+
// });
68+
}
69+
70+
/**
71+
*
72+
* @param {CanvasRenderingContext2D} ctx
73+
*/
74+
render(ctx) {
75+
const dpr = this.canvas.getRetinaScaling();
76+
// ctx.resetTransform();
77+
// ctx.scale(dpr, dpr);
78+
// this.transform(ctx);
79+
const t = this.calcTransformMatrix();
80+
ctx.setTransform(
81+
dpr * t[0],
82+
t[1],
83+
t[2],
84+
dpr * t[3],
85+
dpr * t[4],
86+
dpr * t[5]
87+
);
88+
ctx.strokeRect(-this.width / 2, -this.height / 2, this.width, this.height);
89+
ctx.fillStyle = "white";
90+
ctx.fillRect(-this.width / 2, -this.height / 2, this.width, this.height);
91+
// ctx.beginPath();
92+
// ctx.rect(-this.width / 2, -this.height / 2, this.width, this.height);
93+
// ctx.stroke();
94+
// ctx.fill();
95+
96+
// ctx.beginPath();
97+
// ctx.rect(-this.width / 2, -this.height / 2, this.width, this.height);
98+
// ctx.closePath();
99+
// ctx.fillStyle = "white";
100+
101+
// ctx.stroke();
102+
// ctx.fill();
103+
}
104+
}
105+
4106
class FabricEngine extends Engine {
5107
constructor() {
6108
super();
@@ -21,6 +123,7 @@ class FabricEngine extends Engine {
21123
const r = rects[i];
22124
r.x -= r.speed;
23125
r.el.left = r.x;
126+
delete r.el.m;
24127
if (r.x + r.size < 0) {
25128
r.x = this.width + r.size;
26129
}
@@ -44,7 +147,7 @@ class FabricEngine extends Engine {
44147
const size = 10 + Math.random() * 40;
45148
const speed = 1 + Math.random();
46149

47-
const fRect = new fabric.Rect({
150+
const fRect = new OptimizedRect({
48151
width: size,
49152
height: size,
50153
fill: "white",

0 commit comments

Comments
 (0)