Skip to content

Commit

Permalink
chore: 测试 canvas api 使用情况
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyue committed Jul 25, 2024
1 parent d715549 commit 5faa089
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
18 changes: 18 additions & 0 deletions jest-setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// eslint-disable-next-line
const fs = require('fs');
// eslint-disable-next-line
const { configureToMatchImageSnapshot } = require('jest-image-snapshot');
// eslint-disable-next-line
const CanvasConverter = require('canvas-to-buffer');
Expand All @@ -19,6 +21,22 @@ expect.extend({
const result = toMatchImageSnapshot.call(this, converter.toBuffer());
process.execPath = execPath;

try {
const result = JSON.parse(fs.readFileSync(__dirname + '/result.json', 'utf-8'));
// const result = {};
const commands = received.commands;

if (commands && commands.length) {
commands.forEach((item) => {
result[item[0]] = true;
});
}

fs.writeFileSync(__dirname + '/result.json', JSON.stringify(result));
} catch (err) {
console.error(err);
}

return result;
},
});
72 changes: 72 additions & 0 deletions packages/f2/test/context-collect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const unused = [
'direction',
'filter',
'fontKerning',
'fontStretch',
'fontVariantCaps',
'globalCompositeOperation',
'imageSmoothingEnabled',
'imageSmoothingQuality',
'letterSpacing',
'textRendering',
'getContextAttributes',
'getLineDash',
'getTransform',
'isContextLost',
'isPointInStroke',
'reset',
'roundRect',
];

// 不常用
// const rarelyUsed = [
// 'lineDashOffset',
// 'shadowColor',
// 'shadowOffsetX',
// 'shadowOffsetY',
// 'wordSpacing',
// 'createImageData',
// 'getImageData',
// 'putImageData',
// 'createRadialGradient',
// 'isPointInPath',
// 'quadraticCurveTo',
// 'strokeText',
// 'transform',
// ];
const createCollectContext = (ctx: CanvasRenderingContext2D) => {
const canvas = ctx.canvas;

const commands = [];
const context = new Proxy(ctx, {
get: (target, prop) => {
if (prop === 'commands') {
return commands;
}
// @ts-ignore
if (unused.includes(prop)) {
return;
}
commands.push([prop]);
if (typeof target[prop] === 'function') {
return target[prop].bind(target);
} else {
return target[prop];
}
},
set: function(obj, prop, value) {
commands.push([prop, value]);
obj[prop] = value;
return true;
},
});

// @ts-ignore
canvas.getContext = () => {
return context;
};

return context;
};

export default createCollectContext;
4 changes: 3 additions & 1 deletion packages/f2/test/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import createCollectContext from './context-collect';
const isTouch = (eventType) => {
if (eventType.indexOf('touch') !== -1) return true;
if (eventType.indexOf('press') !== -1) return true;
Expand Down Expand Up @@ -31,7 +32,8 @@ const createContext = (title = '', { width = '300px', height = '225px' } = {}) =
canvasEl.style.width = width;
canvasEl.style.height = height;
document.body.appendChild(canvasEl);
const context = canvasEl.getContext('2d');

const context = createCollectContext(canvasEl.getContext('2d'));
return context;
};

Expand Down
1 change: 1 addition & 0 deletions result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"canvas":true,"clearRect":true,"fillStyle":true,"fillRect":true,"resetTransform":true,"save":true,"beginPath":true,"rect":true,"clip":true,"setTransform":true,"globalAlpha":true,"closePath":true,"fill":true,"restore":true,"arc":true,"font":true,"lineWidth":true,"textAlign":true,"lineJoin":true,"miterLimit":true,"textBaseline":true,"fillText":true,"strokeStyle":true,"moveTo":true,"lineTo":true,"lineCap":true,"stroke":true,"setLineDash":true,"ellipse":true,"bezierCurveTo":true,"createLinearGradient":true,"scale":true,"drawImage":true,"getImageData":true,"measureText":true,"translate":true,"rotate":true,"createPattern":true}

0 comments on commit 5faa089

Please sign in to comment.