Skip to content

Commit ff82f64

Browse files
authored
[css-paint-api] Update "Drawing Images" inside explainer.
1 parent b583bd5 commit ff82f64

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

css-paint-api/EXPLAINER.md

+48-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,54 @@ In this example `performPathPreInit` is doing CPU intensive work which should on
127127
Drawing Images
128128
--------------
129129
130-
TODO
130+
The API works in conjunction with the [CSS Properties and Values](https://drafts.css-houdini.org/css-properties-values-api/) proposal and the [CSS Typed OM](https://drafts.css-houdini.org/css-typed-om/) proposal.
131+
132+
Using the Properties and Values `registerProperty` method, developers can create a custom CSS property with the `<image>` type. E.g.
133+
134+
```js
135+
registerProperty({
136+
name: '--profile-image',
137+
syntax: '<image>'
138+
});
139+
```
140+
141+
This tells the rendering engine to treat the CSS property `--profile-image` as an image; and as a result the style engine will parse and begin loading that image.
142+
143+
You can then directly draw this image from within your paint method:
144+
145+
```js
146+
registerPaint('avatar', class {
147+
static inputProperties = ['--profile-image'];
148+
149+
paint(ctx, size, styleMap) {
150+
// Determine the center point and radius.
151+
const x = size.width / 2;
152+
const y = size.height / 2;
153+
const radius = Math.min(x, y);
154+
155+
ctx.save();
156+
// Set up a round clipping path for the profile image.
157+
ctx.beginPath();
158+
ctx.arc(x, y, radius, 0, 2 * Math.PI, false);
159+
ctx.clip();
160+
161+
// Draw the image inside the round clip.
162+
ctx.drawImage(styleMap.get('--profile-image'));
163+
ctx.restore();
164+
165+
// Draw a badge or something on top of the image.
166+
drawBadge(ctx);
167+
}
168+
});
169+
```
170+
171+
The above example would be used in CSS by:
172+
```css
173+
.avatar-img {
174+
background: paint(avatar);
175+
--profile-image: url("profile-image.png");
176+
}
177+
```
131178
132179
Paint Efficiency
133180
----------------

0 commit comments

Comments
 (0)