Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to provide a public interface for groups #256

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export default (p5: P5SVG) => {
p5.SVGElement = class SVGElement extends p5.Element {
public elt: Element

public isUserInstanciated: boolean

/**
* Returns an Array of children of current SVG Element matching given selector
*
Expand Down Expand Up @@ -50,13 +52,27 @@ export default (p5: P5SVG) => {
* Create SVGElement
*
*/
static create(nodeName: string, attributes: { [key: string]: string }) {
static create(nodeName: string, attributes: { [key: string]: string }, isUserInstanciated: boolean?) {
attributes = attributes || {}
const elt = document.createElementNS('http://www.w3.org/2000/svg', nodeName)
Object.keys(attributes).forEach(function (k) {
elt.setAttribute(k, attributes[k])
})
return new SVGElement(elt as any)
const svgEl = new SVGElement(elt as any)
svgEl.isUserInstanciated = isUserInstanciated;
return svgEl;
}

/**
* Check if any group above is user instanciated
* Will also return true if oneself is user instanciated
*
*/
isWithinUserInstanciated() {
if (this.isUserInstanciated) return true;
if (!(this.parentNode instanceof SVGElement)) return false;

return this.parentNode.isWithinUserInstanciated();
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ export default function (p5: P5SVG) {
// create new <g> so that new element won't be influenced by the filter
g = p5.SVGElement.create('g')
rootGroup.appendChild(g.elt)

if (ctx.__currentElement.isWithinUserInstanciated()) {
console.warn('Filter will promptly exit out of any instanciated group. Please make sure you\'ve exited them before filtering');
}

ctx.__currentElement = g.elt
} else {
_filter.apply(this, [operation, value])
Expand Down
43 changes: 43 additions & 0 deletions src/groupInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import p5RendererSVG from "./p5.RendererSVG";
import { P5SVG, SVGElement, p5SVG } from "./types";

export default function (p5: P5SVG) {
p5.prototype.pushSVGGroup = function (this: p5SVG) {
if (!(this._renderer instanceof p5RendererSVG)) {
console.warn('Attempted to push SVG group in non-svg canvas');
return null;
}

const group = p5.SVGElement.create('g', {}, true);

const currEl = this._renderer.drawingContext.__currentElement;

if (currEl.tagName !== 'g' && currEl.tagName) {
console.warn('Attempted to pop SVG group whilst not in g, svg');
return;
}

currEl.append(group);

this._renderer.drawingContext.__currentElement = group;

return group;
}

p5.prototype.popSVGGroup = function (this: p5SVG, group: SVGElement) {
if (!(this._renderer instanceof p5RendererSVG)) {
console.warn('Attempted to pop SVG group in non-svg canvas');
return null;
}

const currEl = this._renderer.drawingContext.__currentElement;

if (currEl !== group) {
return; // Silently fail: the warning has already been given by filter
}

this._renderer.drawingContext.__currentElement = currEl.parentNode();

return;
}
}
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import IO from './io'
import Image from './image'
import Element from './element'
import Filters from './filters'
import GroupInterface from './groupInterface'
import constants from './constants'

function init(p5: P5) {
Expand All @@ -18,6 +19,8 @@ function init(p5: P5) {
Image(p5svg)
Filters(p5svg)
Element(p5svg)
GroupInterface(p5svg)


// attach constants to p5 instance
p5svg.prototype['SVG'] = constants.SVG
Expand Down