UNPKG

p5

Version:

[![npm version](https://badge.fury.io/js/p5.svg)](https://www.npmjs.com/package/p5)

209 lines (208 loc) • 4.73 kB
//#region src/shape/svg/svg_recorder.js var NodeBase = class { constructor() { this.children = []; } add(child) { this.children.push(child); } }; var ScopeNode = class extends NodeBase { constructor() { super(); this.type = "scope"; } toSVGElement(visitor) { visitor.visitScope(this); } }; var ShapeNode = class extends NodeBase { constructor(shape, state) { super(); this.type = "shape"; this.shape = shape; this.state = state; } toSVGElement(visitor) { visitor.currentState = this.state; visitor.currentPathElement = null; this.shape.accept(visitor); visitor.currentPathElement = null; } }; var BackgroundNode = class extends NodeBase { constructor(color) { super(); this.type = "background"; this.color = color; } toSVGElement(visitor) { visitor.addBackground(this); } }; var ClearNode = class extends NodeBase { constructor() { super(); this.type = "clear"; } toSVGElement(visitor) { visitor.clear(); } }; var ImageNode = class extends NodeBase { constructor(img, args, state) { super(); this.type = "image"; this.img = img; this.args = args; this.state = state; } toSVGElement(visitor) { visitor.currentState = this.state; visitor.visitImage(this); } }; var TransformStack = class { constructor() { this.stack = [new DOMMatrix()]; } push() { this.stack.push(new DOMMatrix(this.current)); } pop() { if (this.stack.length > 1) this.stack.pop(); } translate(x, y) { this.current.translateSelf(x, y); } rotate(rad) { this.current.rotateSelf(rad * 180 / Math.PI); } scale(x, y) { this.current.scaleSelf(x, y !== void 0 ? y : x); } get current() { return this.stack[this.stack.length - 1]; } }; var ShapeRecorder = class { constructor(pInst, options = {}) { this.p5 = pInst; this.active = false; this.draw = options.draw ?? false; this.root = new ScopeNode(); this.scopeStack = [this.root]; this.tStack = new TransformStack(); this.restores = []; this._isTransforming = false; } start() { this.active = true; this.root = new ScopeNode(); this.scopeStack = [this.root]; this.restores = []; this._interceptTransforms(); const renderer = this.p5._renderer; const adapters = this.p5._svgCaptureAdapters(); if (renderer) for (const name in adapters) { const restore = adapters[name].intercept(renderer, this); if (restore) this.restores.push(restore); } } stop() { this.active = false; for (const restore of this.restores) restore(); this.restores = []; } addNode(node) { this.scopeStack[this.scopeStack.length - 1].add(node); } enterScope() { const scope = new ScopeNode(); this.addNode(scope); this.scopeStack.push(scope); return scope; } leaveScope() { if (this.scopeStack.length > 1) this.scopeStack.pop(); } _interceptTransforms() { const p = this.p5; const renderer = p._renderer; const transformHandlers = { push: () => { this.tStack.push(); this.enterScope(); }, pop: () => { this.tStack.pop(); this.leaveScope(); }, translate: (args) => { let x = args[0]; let y = args[1]; if (typeof x === "object" && x !== null && "x" in x && "y" in x) { y = x.y; x = x.x; } this.tStack.translate(x || 0, y || 0); }, rotate: (args) => { this.tStack.rotate(args[0] || 0); }, scale: (args) => { this.tStack.scale(args[0] || 1, args[1]); }, applyMatrix: (args) => { const [a, b, c, d, e, f] = args; this.tStack.current.multiplySelf(new DOMMatrix([ a, b, c, d, e, f ])); }, resetMatrix: () => { const stack = this.tStack.stack; stack[stack.length - 1] = new DOMMatrix(); } }; Object.keys(transformHandlers).forEach((method) => { const applyTransform = (origFn, context, args) => { if (this._isTransforming) return origFn.apply(context, args); this._isTransforming = true; try { if (this.active) transformHandlers[method](args); return origFn.apply(context, args); } finally { this._isTransforming = false; } }; const origP5 = p[method]; if (typeof origP5 === "function") { p[method] = (...args) => { return applyTransform(origP5, p, args); }; this.restores.push(() => { p[method] = origP5; }); } if (renderer && typeof renderer[method] === "function") { const origR = renderer[method]; renderer[method] = (...args) => { return applyTransform(origR, renderer, args); }; this.restores.push(() => { renderer[method] = origR; }); } }); } getRecord() { return this.root; } }; //#endregion export { BackgroundNode, ClearNode, ImageNode, NodeBase, ScopeNode, ShapeNode, ShapeRecorder, TransformStack };