UNPKG

tav-media

Version:

Cross platform media editing framework

85 lines (84 loc) 3.47 kB
import { PAGModule } from '../pag-module'; import { ScalerContext } from './scaler-context'; import { Matrix } from './matrix'; import { getCanvas2D, releaseCanvas2D } from '../utils/canvas'; export class WebMask { constructor(canvas, width, height) { this.canvas = canvas; this.canvas.width = width; this.canvas.height = height; this.context = this.canvas.getContext('2d'); } static create(width, height) { return new WebMask(getCanvas2D(), width, height); } static getLineCap(cap) { switch (cap) { case PAGModule.TGFXLineCap.Round: return 'round'; case PAGModule.TGFXLineCap.Square: return 'square'; default: return 'butt'; } } static getLineJoin(join) { switch (join) { case PAGModule.TGFXLineJoin.Round: return 'round'; case PAGModule.TGFXLineJoin.Bevel: return 'bevel'; default: return 'miter'; } } fillPath(path, fillType) { this.context.setTransform(1, 0, 0, 1, 0, 0); if (fillType === PAGModule.TGFXPathFillType.InverseWinding || fillType === PAGModule.TGFXPathFillType.InverseEvenOdd) { this.context.clip(path, fillType === PAGModule.TGFXPathFillType.InverseEvenOdd ? 'evenodd' : 'nonzero'); this.context.fillRect(0, 0, this.canvas.width, this.canvas.height); } else { this.context.fill(path, fillType === PAGModule.TGFXPathFillType.EvenOdd ? 'evenodd' : 'nonzero'); } } fillText(webFont, texts, positions, matrixWasmIns) { const scalerContext = new ScalerContext(webFont.name, webFont.style, webFont.size, webFont.bold, webFont.italic); const matrix = new Matrix(matrixWasmIns); this.context.setTransform(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty); this.context.font = scalerContext.fontString(); for (let i = 0; i < texts.size(); i++) { const position = positions.get(i); this.context.fillText(texts.get(i), position.x, position.y); } } strokeText(webFont, stroke, texts, positions, matrixWasmIns) { if (stroke.width < 0.5) { return; } const scalerContext = new ScalerContext(webFont.name, webFont.style, webFont.size, webFont.bold, webFont.italic); const matrix = new Matrix(matrixWasmIns); this.context.setTransform(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty); this.context.font = scalerContext.fontString(); this.context.lineJoin = WebMask.getLineJoin(stroke.join); this.context.miterLimit = stroke.miterLimit; this.context.lineCap = WebMask.getLineCap(stroke.cap); this.context.lineWidth = stroke.width; for (let i = 0; i < texts.size(); i++) { const position = positions.get(i); this.context.strokeText(texts.get(i), position.x, position.y); } } clear() { this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); } update(GL) { var _a; const gl = (_a = GL.currentContext) === null || _a === void 0 ? void 0 : _a.GLctx; gl.texImage2D(gl.TEXTURE_2D, 0, gl.ALPHA, gl.ALPHA, gl.UNSIGNED_BYTE, this.canvas); } onDestroy() { releaseCanvas2D(this.canvas); } }