-
-
Notifications
You must be signed in to change notification settings - Fork 17
Porting from p5.js
Quinton Ashley edited this page May 14, 2025
·
54 revisions
If you're already familiar with p5.js, you'll find yourself right at home with q5.js!
Though q5 is largely backwards compatible with p5.js v1.11, we don't aim to fully replicate the p5 or Java Processing APIs. The creation of q5 was an opportunity to add new features, cut outdated cruft, fix misnaming, and modernize aspects of the API.
- q5 supports both p5 v1's
preloadsystem and p5 v2'sasync setuploading with promises. p5 v2 doesn't supportpreload. SeeusePromiseLoadingfor more info. - In p5 v2
P2D_HDRmust be passed as the renderer param tocreateCanvas, but in q5 this is unnecessary since q5's default color space is display-p3 on devices that support HDR. This can be changed viacreateCanvasoptions. - q5 automatically maps RGB, HSL, and HSB colors to the display-p3 gamut on devices that support HDR, p5 v2 lacks this capability.
- In q5, gamut can be specified in the
colorModefunction. In p5 v2, gamut can only specified for RGB formatted colors using the constantRGB_HDR.
- q5's default renderer is called "c2d", it uses CanvasRenderingContext2D (aka Canvas2D), just like P2D mode in p5.
- If a canvas is not created by the user and
noCanvasisn't run before the draw loop starts, then q5 will create a 200x200 canvas automatically. In p5 the default canvas size is 100x100. -
fill,stroke, andbackgroundcan accept any CSS color string in c2d mode. -
colorfunction only parses numeric input, hex, and common named colors. In c2d mode, it will accept, but doesn't parse strings likecolor('hsl(160, 100%, 50%)')into Color objects with color components. -
red,green, andbluefunctions are deprecated. These functions were needed in Processing Java because colors were stored inside a singleintand bitwise operations were necessary to extract color components. In q5, colors are JS objects and their components can be accessed as properties likemyColor.red, which is more idiomatic to JS. -
color.setRed,sound.setVolume, and similar Java style getter/setter functions on objects are deprecated. Set object properties directly, for examplemyColor.red = 200andmySound.volume = 0.5. -
noisefunction's default noise algorithm is perlin noise. p5's default noise is not real perlin noise, it's called blocky noise in q5 and using it requires loading the q5-noisier module. -
tintfunction's second parameter doesn't change the opacity of an image, instead the tint's alpha specifies how strong the tint should be. To dynamically change the opacity of an image, useopacity(globalAlpha). -
imagefunction displays images at their actual size when pixel density is 2 and dimensions are not specified as input parameters. This ensures that images look crisp on modern displays. RundefaultImageScale(1)to match the behavior of p5. See the "Default Sizing of Images" dev log for more info. -
image.trimis a new q5 function that removes transparent pixels from the edges of an image. In p5trim(str)removes whitespace from strings, use JavaScript'sstring.trimfor that in q5. -
image.copycreates a copy of an image, same as callingimage.getwith no parameters. -
inset/image.insetis equivalent to the misnamedcopy/image.copyin p5. It can be used to create detail insets, displaying a subsection of an image at a larger scale. -
createImage,loadImage, andcreateGraphics: as a last parameter to these functions,opt(options) object, users can specify canvas context attributes for an image or graphic.opt.alphais set to true by default. -
loadImageand other load* loading functions do support success callbacks, but their use is deprecated and undocumented, and they don't support a failure callbacks. Usethenandcatchwith promise based loading instead. -
image.pixelDensityis not a function. q5 images can have a pixelDensity but it must be set in the options object when the image is created or loaded. -
login q5 is a shortcut forconsole.log, notMath.logwhich islogein q5.
-
focusedboolean is not implemented correctly in p5 as of v1.11 (it doesn't get updated after the sketch starts), usedocument.hasFocus()instead. -
image.blendfunction, instead use a graphics object, setblendMode, and draw an image -
saveFramesfunction in p5 is terribly slow, by their own admission it can crash browsers if not severely limited. In q5, usesaveCanvaseach draw call to save frames as image files instead. -
saveGiffunction see issue 84
We need your support! Donate via Patreon or GitHub Sponsors.