processing / processing/p5.js

Allow p5.element style() function to accept CSS directly or multiple properties

Open
#6,249 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area:DOM Enhancement
Dominant language
JavaScript
Stars
24k
Forks
3.8k
Avg merge
3d 16h
Merged PRs (30d)
25

Description

Increasing Access

Beginners likely gain some basic HTML/CSS knowledge prior to or parallel to their exposure of p5.js. If they get into the DOMs p5.element set of functions (createDiv(), createButton() etc) – they'll see that it can also be .style()'ized right there - however the format for changing those styles is both very different than the vanilla js approach and the CSS format syntax. Giving the function the ability to accept plain CSS properties, may help one understand the connection between the two.

Most appropriate sub-area of p5.js?
  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • Build Process
  • Unit Testing
  • Internalization
  • Friendly Errors
  • Other (specify if possible)
Feature enhancement details

If using the DOM set of functions extensively to generate dynamic elements on the page with p5.js, one likely has to find a way to style them outside of the p5.element .style() function – which requires one (property, value) per every call. Nevertheless, CSS is usually written as a block of 5+ properties. One technique is to give the elements an id/class and then dynamically add a <style> tag to reference them:

createElement('style', `
  .mydiv{
       background: red;
       border: 1px solid #fff;
   }
`);

However, I wonder if it would help beginners (and experts alike) if the .style() tag also accepted a form of multiple property changes at once? I could imagine two techniques,

1 - block of CSS that is then parsed per : and ; or simply embedded inline
let myStyle = `
    background: red;
    border: 1px solid #fff;
`
createDiv('hello').style(myStyle);
2 - JSON object of property/values
let myStyle = {
    background : 'red',
    border : '1px solid #fff'
}
createDiv('hello').style(myStyle);

Option 1 seems more useful for beginners and reducing the amount of syntax needed. Not sure within implementation, if it would be best that it adds the styling inline or creates similar <style> tag blocks like mentioned above?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the p5.Element.style() entry point in the DOM API and compare its current single-property usage with the proposed CSS-string and JSON-object forms. Resolve which input format and application behavior should be supported, then define completion around an agreed API and verified styling behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.