MyCanvas is a p5.js class that has a HTML canvas that allows multiple canvases on the same web page. Several of the normal p5.js methods (as named in Properties, Methods, and Events listing and in the Testing and Syntax table) have been rewritten to operate in a MyCanvas.
At this point, when writing p5js methods for MyCanvas, very little attention has been paid validating parameters. No "z" parameters have been used. There has been no attempt to make these methods work in the webgl. Documentation is typically quite limited. What is available is on the test pages marked with an "x" in the Testing and Syntax table. Because, the MyCanvas methods are typically similar to those in p5js, one can find further information from the p5js references.
Some applications:
A table showing methods and properties implemented by p5js canvas and MyCanvas.
Download all the myCanvas files as a zip file
Simple example: The result is shown in the div with id = "example" on the right.
function setup() { canvas1 = createMyCanvas(100, 150); canvas1.background(210); canvas1.fill("red"); canvas1.rect(20, 40, 60, 50); canvas1.text("A MyCanvas", 10, 130); canvas2 = createMyCanvas(150, 100); canvas2.background(200, 200, 255); canvas2.text("Another MyCanvas", 10, 90); canvas2.stroke("#008000"); canvas2.line(20, 10, 130, 90); canvas1.parent("example"); canvas2.parent("example"); } | |
When using MyCanvases, the most common error is to forgetting to prefix the method name, mouseX, and mouseY with the canvas name.
InitializationcreateMyCanvasPropertiesmouseXmouseY isOver * mouseIsPressed keyIsPressed elt canvas touches Data_backgroundColor*_colorMode _colorMaxes _fillColor* _rectMode _ellipseMode _fillColor* _strokeColor* _textSize _textFont _textLeading _textStyle _myMainCanvas_myMainCanvasmyBackground myRect |
Methodsidparent child resizeCanvas color colorMode style position center html fill noFill stroke noStroke strokeWeight strokeCap background clear point line rect rectMode ellipse ellipseMode arc triangle quad square circle bezier bezierPoint bezierTangent text textSize textAlign (horizontal alignment only) textFont textStyle rotate translate scale convertImageToMyCanvas* hide show loadImage image browse* applyMatrix resetMatrix beginShape endShape vertex |
EventsmousePressedmouseReleased mouseClicked doubleClicked mouseWheel mouseMoved mouseOver mouseOut keyDown* keyUp* touchStarted touchMoved touchEnded dragOver dragLeave drop |
* not a standard p5.js property, method or event.
Key: u - method is used. x - primary example, test, syntax, and some documentation comments. The links are to .html files with that name.
Method or Property | shapeText | transforms | bezier | colors | events | domImage | myMain | child | moreTests |
---|---|---|---|---|---|---|---|---|---|
createMyCanvas (Number of MyCanvas) |
u (3) | u (1) | u (2) | u (1) | u (1) | x (3) | u (2) | u (3) | u (2) |
_myMainCanvas | x | ||||||||
myBackground | x | ||||||||
myRect | x | ||||||||
id | x | ||||||||
parent | u | u | u | u | u | x | |||
child | x | ||||||||
resizeCanvas | x | ||||||||
color | x | ||||||||
colorMode | colorModeTest.html | ||||||||
style | x | ||||||||
position | x | ||||||||
center | x | ||||||||
html | x | ||||||||
fill, noFill stroke, noStroke |
u | u | u | x | u | u | |||
strokeWeight | x | u | |||||||
strokeCap | x | ||||||||
background | u | u | u | x | u | u | u | u | u |
clear | x | ||||||||
point | x | ||||||||
rect | x | u | u | u | u | u | u | u | |
rectMode | x | ||||||||
line | x | u | u | ||||||
ellipse | x | u | u | u | |||||
ellipseMode | x | ||||||||
arc | x | ||||||||
triangle quad |
x | ||||||||
circle square |
x | ||||||||
bezier bezierPoint bezierTangent | x | ||||||||
text | x | u | u | u | u | u | u | ||
textSize | x | u | u | ||||||
textAlign textFont textStyle |
x | ||||||||
rotate translate scale |
x | ||||||||
hide | x | ||||||||
show | x | ||||||||
convertImageToMyCanvas* | x | ||||||||
loadImage | x | ||||||||
image | u | u | x | ||||||
browse* | x | ||||||||
applyMatrix resetMatrix |
x | ||||||||
cbeginShape endShape vertex |
shapesTest.html | ||||||||
mousePressed mouseReleased mouseMoved |
u | x | |||||||
doubleClicked mouseWheel mouseOver mouseOver mouseOut |
x | ||||||||
keyDown * keyUp * |
x | ||||||||
touchStarted touchMoved touchEnded |
x | ||||||||
dragOver dragLeave |
x | ||||||||
drop | x | ||||||||
mouseX mouseY |
u | x | |||||||
isOver * mouseIsPressed keyIsPressed |
x | ||||||||
elt | x | ||||||||
canvas | x | ||||||||
x |
* not a standard p5.js property or event.
The p5.js file: myCanvas.pjs
James Brink, 07/06/2020