123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611 |
- // @ts-check
- export default class WxCanvas {
- ctx;
- type;
- canvasId;
- canvasNode;
- stepList = [];
- canvasPrototype = {};
- constructor(type, ctx, canvasId, isNew, canvasNode) {
- this.ctx = ctx;
- this.canvasId = canvasId;
- this.type = type;
- if (isNew) {
- this.canvasNode = canvasNode || {};
- }
- }
- set width(w) {
- if (this.canvasNode) this.canvasNode.width = w;
- }
- get width() {
- if (this.canvasNode) return this.canvasNode.width;
- return 0;
- }
- set height(h) {
- if (this.canvasNode) this.canvasNode.height = h;
- }
- get height() {
- if (this.canvasNode) return this.canvasNode.height;
- return 0;
- }
- set lineWidth(args) {
- this.canvasPrototype.lineWidth = args;
- this.stepList.push({
- action: "lineWidth",
- args,
- actionType: "set",
- });
- }
- get lineWidth() {
- return this.canvasPrototype.lineWidth;
- }
- set lineCap(args) {
- this.canvasPrototype.lineCap = args;
- this.stepList.push({
- action: "lineCap",
- args,
- actionType: "set",
- });
- }
- get lineCap() {
- return this.canvasPrototype.lineCap;
- }
- set lineJoin(args) {
- this.canvasPrototype.lineJoin = args;
- this.stepList.push({
- action: "lineJoin",
- args,
- actionType: "set",
- });
- }
- get lineJoin() {
- return this.canvasPrototype.lineJoin;
- }
- set miterLimit(args) {
- this.canvasPrototype.miterLimit = args;
- this.stepList.push({
- action: "miterLimit",
- args,
- actionType: "set",
- });
- }
- get miterLimit() {
- return this.canvasPrototype.miterLimit;
- }
- set lineDashOffset(args) {
- this.canvasPrototype.lineDashOffset = args;
- this.stepList.push({
- action: "lineDashOffset",
- args,
- actionType: "set",
- });
- }
- get lineDashOffset() {
- return this.canvasPrototype.lineDashOffset;
- }
- set font(args) {
- this.canvasPrototype.font = args;
- this.ctx.font = args;
- this.stepList.push({
- action: "font",
- args,
- actionType: "set",
- });
- }
- get font() {
- return this.canvasPrototype.font;
- }
- set textAlign(args) {
- this.canvasPrototype.textAlign = args;
- this.stepList.push({
- action: "textAlign",
- args,
- actionType: "set",
- });
- }
- get textAlign() {
- return this.canvasPrototype.textAlign;
- }
- set textBaseline(args) {
- this.canvasPrototype.textBaseline = args;
- this.stepList.push({
- action: "textBaseline",
- args,
- actionType: "set",
- });
- }
- get textBaseline() {
- return this.canvasPrototype.textBaseline;
- }
- set fillStyle(args) {
- this.canvasPrototype.fillStyle = args;
- this.stepList.push({
- action: "fillStyle",
- args,
- actionType: "set",
- });
- }
- get fillStyle() {
- return this.canvasPrototype.fillStyle;
- }
- set strokeStyle(args) {
- this.canvasPrototype.strokeStyle = args;
- this.stepList.push({
- action: "strokeStyle",
- args,
- actionType: "set",
- });
- }
- get strokeStyle() {
- return this.canvasPrototype.strokeStyle;
- }
- set globalAlpha(args) {
- this.canvasPrototype.globalAlpha = args;
- this.stepList.push({
- action: "globalAlpha",
- args,
- actionType: "set",
- });
- }
- get globalAlpha() {
- return this.canvasPrototype.globalAlpha;
- }
- set globalCompositeOperation(args) {
- this.canvasPrototype.globalCompositeOperation = args;
- this.stepList.push({
- action: "globalCompositeOperation",
- args,
- actionType: "set",
- });
- }
- get globalCompositeOperation() {
- return this.canvasPrototype.globalCompositeOperation;
- }
- set shadowColor(args) {
- this.canvasPrototype.shadowColor = args;
- this.stepList.push({
- action: "shadowColor",
- args,
- actionType: "set",
- });
- }
- get shadowColor() {
- return this.canvasPrototype.shadowColor;
- }
- set shadowOffsetX(args) {
- this.canvasPrototype.shadowOffsetX = args;
- this.stepList.push({
- action: "shadowOffsetX",
- args,
- actionType: "set",
- });
- }
- get shadowOffsetX() {
- return this.canvasPrototype.shadowOffsetX;
- }
- set shadowOffsetY(args) {
- this.canvasPrototype.shadowOffsetY = args;
- this.stepList.push({
- action: "shadowOffsetY",
- args,
- actionType: "set",
- });
- }
- get shadowOffsetY() {
- return this.canvasPrototype.shadowOffsetY;
- }
- set shadowBlur(args) {
- this.canvasPrototype.shadowBlur = args;
- this.stepList.push({
- action: "shadowBlur",
- args,
- actionType: "set",
- });
- }
- get shadowBlur() {
- return this.canvasPrototype.shadowBlur;
- }
- save() {
- this.stepList.push({
- action: "save",
- args: null,
- actionType: "func",
- });
- }
- restore() {
- this.stepList.push({
- action: "restore",
- args: null,
- actionType: "func",
- });
- }
- setLineDash(...args) {
- this.canvasPrototype.lineDash = args;
- this.stepList.push({
- action: "setLineDash",
- args,
- actionType: "func",
- });
- }
- moveTo(...args) {
- this.stepList.push({
- action: "moveTo",
- args,
- actionType: "func",
- });
- }
- closePath() {
- this.stepList.push({
- action: "closePath",
- args: null,
- actionType: "func",
- });
- }
- lineTo(...args) {
- this.stepList.push({
- action: "lineTo",
- args,
- actionType: "func",
- });
- }
- quadraticCurveTo(...args) {
- this.stepList.push({
- action: "quadraticCurveTo",
- args,
- actionType: "func",
- });
- }
- bezierCurveTo(...args) {
- this.stepList.push({
- action: "bezierCurveTo",
- args,
- actionType: "func",
- });
- }
- arcTo(...args) {
- this.stepList.push({
- action: "arcTo",
- args,
- actionType: "func",
- });
- }
- arc(...args) {
- this.stepList.push({
- action: "arc",
- args,
- actionType: "func",
- });
- }
- rect(...args) {
- this.stepList.push({
- action: "rect",
- args,
- actionType: "func",
- });
- }
- scale(...args) {
- this.stepList.push({
- action: "scale",
- args,
- actionType: "func",
- });
- }
- rotate(...args) {
- this.stepList.push({
- action: "rotate",
- args,
- actionType: "func",
- });
- }
- translate(...args) {
- this.stepList.push({
- action: "translate",
- args,
- actionType: "func",
- });
- }
- transform(...args) {
- this.stepList.push({
- action: "transform",
- args,
- actionType: "func",
- });
- }
- setTransform(...args) {
- this.stepList.push({
- action: "setTransform",
- args,
- actionType: "func",
- });
- }
- clearRect(...args) {
- this.stepList.push({
- action: "clearRect",
- args,
- actionType: "func",
- });
- }
- fillRect(...args) {
- this.stepList.push({
- action: "fillRect",
- args,
- actionType: "func",
- });
- }
- strokeRect(...args) {
- this.stepList.push({
- action: "strokeRect",
- args,
- actionType: "func",
- });
- }
- fillText(...args) {
- this.stepList.push({
- action: "fillText",
- args,
- actionType: "func",
- });
- }
- strokeText(...args) {
- this.stepList.push({
- action: "strokeText",
- args,
- actionType: "func",
- });
- }
- beginPath() {
- this.stepList.push({
- action: "beginPath",
- args: null,
- actionType: "func",
- });
- }
- fill() {
- this.stepList.push({
- action: "fill",
- args: null,
- actionType: "func",
- });
- }
- stroke() {
- this.stepList.push({
- action: "stroke",
- args: null,
- actionType: "func",
- });
- }
- drawFocusIfNeeded(...args) {
- this.stepList.push({
- action: "drawFocusIfNeeded",
- args,
- actionType: "func",
- });
- }
- clip() {
- this.stepList.push({
- action: "clip",
- args: null,
- actionType: "func",
- });
- }
- isPointInPath(...args) {
- this.stepList.push({
- action: "isPointInPath",
- args,
- actionType: "func",
- });
- }
- drawImage(...args) {
- this.stepList.push({
- action: "drawImage",
- args,
- actionType: "func",
- });
- }
- addHitRegion(...args) {
- this.stepList.push({
- action: "addHitRegion",
- args,
- actionType: "func",
- });
- }
- removeHitRegion(...args) {
- this.stepList.push({
- action: "removeHitRegion",
- args,
- actionType: "func",
- });
- }
- clearHitRegions(...args) {
- this.stepList.push({
- action: "clearHitRegions",
- args,
- actionType: "func",
- });
- }
- putImageData(...args) {
- this.stepList.push({
- action: "putImageData",
- args,
- actionType: "func",
- });
- }
- getLineDash() {
- return this.canvasPrototype.lineDash;
- }
- createLinearGradient(...args) {
- return this.ctx.createLinearGradient(...args);
- }
- createRadialGradient(...args) {
- if (this.type === "2d") {
- return this.ctx.createRadialGradient(...args);
- } else {
- return this.ctx.createCircularGradient(...args.slice(3, 6));
- }
- }
- createPattern(...args) {
- return this.ctx.createPattern(...args);
- }
- measureText(...args) {
- return this.ctx.measureText(...args);
- }
- createImageData(...args) {
- return this.ctx.createImageData(...args);
- }
- getImageData(...args) {
- return this.ctx.getImageData(...args);
- }
- async draw(reserve, func) {
- const realstepList = this.stepList.slice();
- this.stepList.length = 0;
- if (this.type === "mina") {
- if (realstepList.length > 0) {
- for (const step of realstepList) {
- this.implementMinaStep(step);
- }
- this.ctx.draw(reserve, func);
- realstepList.length = 0;
- }
- } else if (this.type === "2d") {
- if (!reserve) {
- this.ctx.clearRect(0, 0, this.canvasNode.width, this.canvasNode.height);
- }
- if (realstepList.length > 0) {
- for (const step of realstepList) {
- await this.implement2DStep(step);
- }
- realstepList.length = 0;
- }
- if (func) {
- func();
- }
- }
- realstepList.length = 0;
- }
- implementMinaStep(step) {
- switch (step.action) {
- case "textAlign": {
- this.ctx.setTextAlign(step.args);
- break;
- }
- case "textBaseline": {
- this.ctx.setTextBaseline(step.args);
- break;
- }
- default: {
- if (step.actionType === "set") {
- this.ctx[step.action] = step.args;
- } else if (step.actionType === "func") {
- if (step.args) {
- this.ctx[step.action](...step.args);
- } else {
- this.ctx[step.action]();
- }
- }
- break;
- }
- }
- }
- implement2DStep(step) {
- return new Promise((resolve) => {
- if (step.action === "drawImage") {
- const img = this.canvasNode.createImage();
- img.src = step.args[0];
- img.onload = () => {
- this.ctx.drawImage(img, ...step.args.slice(1));
- resolve();
- };
- } else {
- if (step.actionType === "set") {
- this.ctx[step.action] = step.args;
- } else if (step.actionType === "func") {
- if (step.args) {
- this.ctx[step.action](...step.args);
- } else {
- this.ctx[step.action]();
- }
- }
- resolve();
- }
- });
- }
- }
|