Alpha Paintlet

Dave Rupert created a little lovely Houdini Paint Worklet that allows you set a background color with an alpha on elements. The Worklet’s code is as simple as this: registerPaint(‘alpha’, class { static get inputProperties() { return [‘–bg-alpha’, ‘–bg-color’] } paint(ctx, size, props) { ctx.globalAlpha = props.get(‘–bg-alpha’); ctx.fillStyle = props.get(‘–bg-color’); ctx.fillRect(0, 0, size.width, size.height); } …


This content originally appeared on Bram.us and was authored by Bramus!

Dave Rupert created a little lovely Houdini Paint Worklet that allows you set a background color with an alpha on elements. The Worklet’s code is as simple as this:

registerPaint('alpha', class {
  static get inputProperties() {
    return ['--bg-alpha', '--bg-color']
  }

  paint(ctx, size, props) {
    ctx.globalAlpha = props.get('--bg-alpha');
    ctx.fillStyle = props.get('--bg-color');
    ctx.fillRect(0, 0, size.width, size.height);
  }
})
🎩 Houdini, ain't that a magician?

Houdini is a set of low-level APIs that exposes parts of the CSS engine, giving developers the power to extend CSS by hooking into the styling and layout process of a browser’s rendering engine. Houdini is a group of APIs that give developers direct access to the CSS Object Model (CSSOM), enabling developers to write code the browser can parse as CSS, thereby creating new CSS features without waiting for them to be implemented natively in browsers.

It really is magic, hence it's name Houdini. I'd recommend this slidedeck and this video to get you started

Using the two custom properties --bg-alpha and --bg-color you can control how it should look (in browsers that support Houdini, i.e. Chromium).

See the Pen
Alpha Paintlet Demo
by Dave Rupert (@davatron5000)
on CodePen.

~

In the video below you can see the conversation that builds up to it.

~

🎨 Alpha Paintlet (Blogpost) →
alpha-paintlet Source (GitHub) →


This content originally appeared on Bram.us and was authored by Bramus!


Print Share Comment Cite Upload Translate Updates
APA

Bramus! | Sciencx (2021-10-21T23:44:06+00:00) Alpha Paintlet. Retrieved from https://www.scien.cx/2021/10/21/alpha-paintlet/

MLA
" » Alpha Paintlet." Bramus! | Sciencx - Thursday October 21, 2021, https://www.scien.cx/2021/10/21/alpha-paintlet/
HARVARD
Bramus! | Sciencx Thursday October 21, 2021 » Alpha Paintlet., viewed ,<https://www.scien.cx/2021/10/21/alpha-paintlet/>
VANCOUVER
Bramus! | Sciencx - » Alpha Paintlet. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/21/alpha-paintlet/
CHICAGO
" » Alpha Paintlet." Bramus! | Sciencx - Accessed . https://www.scien.cx/2021/10/21/alpha-paintlet/
IEEE
" » Alpha Paintlet." Bramus! | Sciencx [Online]. Available: https://www.scien.cx/2021/10/21/alpha-paintlet/. [Accessed: ]
rf:citation
» Alpha Paintlet | Bramus! | Sciencx | https://www.scien.cx/2021/10/21/alpha-paintlet/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.