forked from amazingphilippe/dynamic-css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.html
837 lines (722 loc) · 26 KB
/
js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Dynamic CSS Masterclass: JS</title>
<link href="https://inspirejs.org/inspire.css" rel="stylesheet" />
<link href="https://inspirejs.org/theme.css" rel="stylesheet" />
<link href="https://projects.verou.me/talks/shared/intro-outro.css" rel="stylesheet" />
<link href="css/prism.css" rel="stylesheet" />
<link href="css/theme.css" rel="stylesheet" />
<link href="css/talk.css" rel="stylesheet" />
<link href="https://projects.verou.me/talks/shared/browser-support.css" rel="stylesheet" />
</head>
<body class="language-javascript"
data-markdown-elements="details.notes, .md, .takeaway.slide > h1, .credit"
data-balance-elements=".slide > h1:only-child, .takeaway.slide > h1, .speech.slide > h1"
data-load-plugins="docs">
<header id="intro" class="slide dont-resize">
<h1>Dynamic CSS Masterclass</h1>
<h2>Day 5: JS</h2>
<p class="attribution">By <a href="http://lea.verou.me">Lea Verou</a> (<a href="http://twitter.com/leaverou">@LeaVerou</a>)</p>
</header>
<article class="slide allow-attendee">
<div>
<pre><code>
// Get variable from inline style
element.style.getPropertyValue("--foo");
</code></pre>
<pre class="delayed"><code>
// Get variable from wherever
getComputedStyle(element).getPropertyValue("--foo");
</code></pre>
<pre class="delayed"><code>
// Set variable on inline style
element.style.setProperty("--foo", 38 + 4);
</code></pre>
</div>
</article>
<section>
<header class="slide" style="--icon: 🐭; background-color: var(--accent2);">
<h1>Mouse position</h1>
</header>
<!-- <article class="slide language-javascript">
<p><a href="https://mavo.io"><img src="https://projects.verou.me/html-reimagined/img/logo.svg" alt="Mavo" style="height: 4em"></a></p>
<pre><code class="language-markup"><html mv-app
style="--mouse-x: [$mouse.x / innerWidth];
--mouse-y: [$mouse.y / innerHeight];"></code></pre>
</article> -->
<article class="isolated horizontal demo slide" id="mouse">
<textarea class="language-css">
:root {
background-image: radial-gradient(
at 50% 50%,
transparent, black);
min-height: 100vh;
}
</textarea>
<textarea class="language-js">
let root = document.documentElement;
document.addEventListener("pointermove", evt => {
/*
evt.clientX and evt.clientY are the mouse coordinates
innerWidth and innerHeight give you viewport dimensions
*/
root.style.setProperty("--mouse-x", 0);
root.style.setProperty("--mouse-y", 0);
});
</textarea>
</article>
<article class="isolated horizontal demo slide" id="svg-eyes">
<textarea class="language-css">
.iris {
cx: calc(25px * var(--look, 0) + 50px);
}
</textarea>
<textarea class="language-html">
<svg viewBox="0 0 250 100">
<clipPath id="white-clip">
<path id="white-path" d="M1,50 a65,80 0 0,1 100,0 a80,80 0 0,1 -100,0 Z" />
</clipPath>
<use id="eye-white" href="#white-path" />
<use href="#eye-white" x="148" />
<circle id="iris" class="iris" cx="50" cy="45" r="18" clip-path="url(#white-clip)" />
<use class="iris" href="#iris" x="148" />
</svg>
</textarea>
<textarea class="language-js">
let root = document.documentElement;
document.addEventListener("pointermove", evt => {
root.style.setProperty("--mouse-x", evt.clientX);
root.style.setProperty("--mouse-y", evt.clientY);
});
</textarea>
<style data-slide media="not all" class="demo">
:root {
background: hsl(20, 40%, 70%);
}
svg {
width: 90vw;
}
#eye-white {
fill: white;
}
.iris {
stroke: hsl(200, 50%, 50%);
stroke-width: 14;
fill: black;
}
.second-eye {
x: 150px;
}
</style>
<details class="notes">
If we set `--mouse-x` and `--mouse-y` based on pixels,
what happens here? How can we set `--look` based on that?
Setting things based on pixels makes them less reusable.
What if we instead set `--mouse-x` and `--mouse-y`
based on percentage of screen?
--------
Then we can still get a length by multiplying with `100vw`
and `100vh` respectively, but we can also just use
the percentage directly.
</details>
</article>
<article class="slide allow-attendee">
<h1>Reusable pointer coordinates</h1>
<pre><code>
let root = document.documentElement;
document.addEventListener("pointermove", evt => {
let x = evt.clientX / innerWidth;
let y = evt.clientY / innerHeight;
root.style.setProperty("--mouse-x", x);
root.style.setProperty("--mouse-y", y);
});
</code></pre>
</article>
<article class="slide" data-insert="svg-eyes"></article>
<article class="takeaway slide">
<h1>Prefer abstract 0-1 percentages than absolute pixel lengths</h1>
</article>
<article class="slide language-css">
<div>
<p>0-1 can be converted to a length:</p>
<pre><code>calc(var(--mouse-x) * 100vw)</code></pre>
<p>…but the reverse isn’t possible</p>
</div>
</article>
<article class="question slide">
<h1>What about local coordinates?</h1>
</article>
<article class="slide allow-attendee">
<h1>Getting local pointer coordinates</h1>
<pre><code>
let rect = evt.target.getBoundingClientRect();
let top = evt.clientY - rect.top;
let left = evt.clientX - rect.left;
<span class="delayed">let x = left / rect.width;
let y = top / rect.height;
evt.target.style.setProperty("--mouse-local-x", x);
evt.target.style.setProperty("--mouse-local-y", y);</span>
</code></pre>
</article>
<!--
<article class="isolated demo slide">
<textarea class="language-css">
</textarea>
<textarea class="language-html">
</textarea>
<textarea class="language-js">
</textarea>
<style data-slide media="not all" class="demo">
</style>
<details class="notes">
</details>
</article>
-->
<article class="isolated horizontal demo slide no-base-css" id="mouse-local">
<textarea class="language-css">
button {
background-color: rebeccapurple;
}
button:active {
background-image: radial-gradient(
circle at calc(100% * var(--mouse-local-x)) calc(100% * var(--mouse-local-y)),
hsl(0 100% 100% / .4), transparent
);
}
</textarea>
<textarea class="language-html">
<button>Hello</button>
</textarea>
<textarea class="language-js">
document.addEventListener("pointerdown", evt => {
let target = evt.target;
let rect = target.getBoundingClientRect();
let x = (evt.clientX - rect.left) / rect.width;
let y = (evt.clientY - rect.top) / rect.height;
target.style.setProperty("--mouse-local-x", x);
target.style.setProperty("--mouse-local-y", y);
});
</textarea>
<style data-slide media="not all" class="demo">
button {
padding: .5em .8em;
border-radius: .2em;
border: 0;
font: 200%/1 Helvetica Neue, Segoe UI, sans-serif;
color: white;
}
</style>
<details class="notes">
Here we are generically setting local pointer coordinates on an element.
Let's change the background gradient to draw a circle.
Note that we did not need to change anything in the JS.
Let's now change the code to draw this effect as the pointer moves over the element
([solution](https://codepen.io/leaverou/pen/56f9b2ff238636c5117d87436fb55c9b)).
</details>
</article>
<article class="md slide">
# A note on performance
- Event delegation (monitoring events on `document`)
gives you maximum flexibility and maintainability, but can be slower
- Scoping to specific (few) elements can be more performant, but is a tighter coupling of CSS and JS
- For anything other than `*move`, err on flexibility
- For `*move`, it depends. I'd err on flexibility, measure, adjust if needed.
<details class="notes">
See also [this article](https://nolanlawson.com/2019/08/11/high-performance-input-handling-on-the-web/).
</details>
</article>
<article class="codepen slide" id="ripple-effect">
<iframe src="https://codepen.io/leaverou/embed/preview/acb3c50c99c89ed9aec9f141c6b1653a?height=265&theme-id=dark&default-tab=html,result&editable=true" loading="lazy"></iframe>
</article>
<article class="slide allow-attendee">
<h1>More 0-1 Range Mapping</h1>
<p>Remember <code>calc((max - min) * var(--p) + min)</code>?
<ol class="math">
<li>(max - min) × p + min</li>
<li>max × p - min × p + min</li>
<li>max × p + (1 - p) × min</li>
</ol>
<p>What does <code>calc(var(--p) * max + (1 - var(--p)) * min)</code> remind you?</p>
</article>
<article class="slide" data-insert="#mouse-local"></article>
</section>
<section>
<header class="slide" style="--icon: 👗; background-color: var(--accent3)">
<h1>Forms</h1>
</header>
<article class="slide allow-attendee">
<div><pre><code>
for (input of document.querySelectorAll("input")) {
input.style.setProperty("--value", input.value);
}
</code></pre>
<pre class="delayed"><code>
document.addEventListener("input", evt => {
let input = evt.target;
input.style.setProperty("--value", input.value);
});
</code></pre></div>
</article>
<article class="horizontal isolated demo slide" id="slider">
<textarea class="language-css">
input {
appearance: none;
background: linear-gradient(to right, #f06 50%, hsl(220 10% 90%) 0);
}
</textarea>
<textarea class="language-js">
for (input of document.querySelectorAll("input")) {
input.style.setProperty("--value", input.value);
}
document.addEventListener("input", evt => {
let input = evt.target;
input.style.setProperty("--value", input.value);
});
</textarea>
<style class="demo" data-slide>
input {
width: 300px;
-webkit-appearance: none;
transform: scale(3);
transform-origin: top left;
border-radius: .1em;
}
</style>
<textarea class="language-html"><input type="range" /></textarea>
</article>
</section>
<section>
<header class="slide" style="--icon: 💬">
<h1>Content-dependent effects</h1>
</header>
<article class="horizontal isolated demo no-base-css slide" id="typing">
<textarea class="language-css">
@keyframes typing { from { width: 0; } }
@keyframes caret { 50% { border-color: transparent; } }
.typing {
width: 25ch;
border-right: 2px solid;
overflow: hidden;
animation: caret .5s step-end infinite alternate;
}
</textarea>
<textarea class="language-html">
<h1 class="typing" contentEditable>CSS variables are awesome</h1>
</textarea>
<textarea class="language-js">
</textarea>
<style data-slide media="not all" class="demo">
:root {
background: hsl(200, 10%, 25%);
}
.typing {
white-space: nowrap;
font: bold 400% Consolas, Monaco, monospace;
color: white;
}
</style>
<details class="notes">
Let’s try to recreate a common "typewriter" effect for this heading.
Our width is set in `ch` units. `1ch` is the width of the `0` glyph.
That is not super useful in general, but in monospace fonts,
that gives us the width of *every* character.
So, we have managed to recreate the effect, but we had to hardcode the length of the heading.
CSS variables can help us make this more widely useful.
</details>
</article>
<article class="slide allow-attendee">
<h1>Passing element content length</h1>
<pre><code>
for (let element of document.querySelectorAll(".typing")) {
let length = element.textContent.length;
element.style.setProperty("--length", length);
}
</code></pre>
</article>
<article class="slide" data-insert="typing"></article>
<article class="question slide">
<h1>What happens if content can change after load?</h1>
</article>
<article class="horizontal isolated demo no-base-css slide" id="typing">
<textarea class="language-css">
@keyframes typing { from { width: 0; } }
@keyframes caret { 50% { border-color: transparent; } }
.typing {
width: calc(1ch * var(--length));
border-right: 2px solid;
overflow: hidden;
animation: caret .5s step-end infinite alternate,
typing calc(.3s * var(--length)) steps(var(--length));
}
</textarea>
<textarea class="language-html">
<h1 class="typing" contentEditable>CSS variables are awesome</h1>
</textarea>
<textarea class="language-js">
for (let element of document.querySelectorAll(".typing")) {
let length = element.textContent.length;
element.style.setProperty("--length", length);
}
</textarea>
<style data-slide media="not all" class="demo">
:root {
background: hsl(200, 10%, 25%);
}
.typing {
white-space: nowrap;
font: bold 400% Consolas, Monaco, monospace;
color: white;
}
</style>
<details class="notes">
Let’s try to recreate a common "typewriter" effect for this heading.
Our width is set in `ch` units. `1ch` is the width of the `0` glyph.
That is not super useful in general, but in monospace fonts,
that gives us the width of *every* character.
So, we have managed to recreate the effect, but we had to hardcode the length of the heading.
CSS variables can help us make this more widely useful.
</details>
</article>
<article class="slide allow-attendee">
<h1>Mutation Observers to the rescue!</h1>
<pre><code>
let observer = new MutationObserver(records => {
for (let r of records) {
let element = r.target;
<span class="delayed">if (element.nodeType === Node.TEXT_NODE) {
element = element.parentNode;
}</span>
let length = element.textContent.length;
element.style.setProperty("--length", length);
}
});
</code></pre>
</article>
<article class="slide allow-attendee">
<h1>Mutation Observers to the rescue!</h1>
<pre><code>
for (let element of document.querySelectorAll(".typing")) {
let length = element.textContent.length;
element.style.setProperty("--length", length);
<span class="delayed">observer.observe(element, {
characterData: true,
childNodes: true,
subtree: true
});</span>
}
</code></pre>
</article>
</section>
<section>
<header class="slide" style="--icon: 📜">
<h1>Scrolling</h1>
</header>
<article class="slide allow-attendee">
<pre><code>
document.addEventListener("scroll", evt => {
let el = evt.target;
<span class="delayed">let maxScroll = el.scrollHeight - el.offsetHeight;</span>
let scroll = el.scrollTop <span class="delayed">/ maxScroll;</span>
el.style.setProperty("--scroll", scroll);
}, {capture: true});
</code></pre>
</article>
<article class="horizontal isolated demo slide" id="scrolling">
<textarea class="language-css">
.scrolling {
background-image: linear-gradient(to right,
hsl(220 10% 90%) 20%, transparent 0)
}
</textarea>
<style data-slide class="demo">
.scrolling {
overflow: auto;
max-height: 50vh;
border: .2em solid white;
background: hsla(0,0%,100%,.2) no-repeat;
text-align: left;
padding: .5em 1em;
font: 100% Baskerville, Palatino, Georgia, serif;
}
</style>
<textarea class="language-js">
document.addEventListener("scroll", evt => {
let el = evt.target;
let maxScroll = el.scrollHeight - el.offsetHeight;
let scroll = el.scrollTop / maxScroll;
el.style.setProperty("--scroll", scroll);
}, {capture: true});
</textarea>
<textarea class="language-html">
<div class="scrolling">
<p>The allowed syntax for custom properties is extremely permissive. The <declaration-value> production matches any sequence of one or more tokens, so long as the sequence does not contain <bad-string-token>, <bad-url-token>, unmatched <)-token>, <]-token>, or <}-token>, or top-level <semicolon-token> tokens or <delim-token> tokens with a value of "!".</p>
<p>In addition, if the value of a custom property contains a var() reference, the var() reference must be valid according to the specified var() grammar. If not, the custom property is invalid and must be ignored.</p>
<p>The values of custom properties, and the values of var() functions substituted into custom properties, are case-sensitive, and must be preserved in their original author-given casing. (Many CSS values are ASCII case-insensitive, which user agents can take advantage of by "canonicalizing" them into a single casing, but that isn’t allowed for custom properties.)
<p>The initial value of a custom property is an empty value; that is, nothing at all. This initial value has a special interaction with the var() notation, which is explained in the section defining var().
<p>Custom properties are ordinary properties, so they can be declared on any element, are resolved with the normal inheritance and cascade rules, can be made conditional with @media and other conditional rules, can be used in HTML’s style attribute, can be read or set using the CSSOM, etc.
<p>Notably, they can even be transitioned or animated, but since the UA has no way to interpret their contents, they always use the "flips at 50%" behavior that is used for any other pair of values that can’t be intelligently interpolated. However, any custom property used in a @keyframes rule becomes animation-tainted, which affects how it is treated when referred to via the var() function in an animation property.</p>
</div>
</textarea>
</article>
<article class="md slide">
# A note on performance
- This has been fun, but event delegation on `scroll` is *usually* a Bad Idea™
- Trade in some flexibility for performance and scope it to specific elements
<details class="notes">
See also [this article](https://nolanlawson.com/2019/08/11/high-performance-input-handling-on-the-web/).
</details>
</article>
</section>
<article class="takeaway slide">
<h1>CSS Variables are a revolution for <br>separation of style and behavior</h1>
</article>
<section>
<header class="slide" style="--icon: 🎨; background-color: var(--accent4)">
<h1>Houdini Paint API</h1>
</header>
<article class="question slide">
<h1>What is it for?</h1>
</article>
<article class="takeaway slide">
<h1>The Paint API is for creating custom CSS `<image>` values</h1>
<details class="notes">
Remember how once `conic-gradient()` was introduced, we had to wait
years until it got support across the board?
The idea of the Paint API is to provide a mechanism with which
developers can create their own dynamic graphics to use as CSS images
instead of waiting for browsers to support native syntax for them.
</details>
</article>
<article class="slide language-css">
<h1>The vision</h1>
<div>
<pre><code>
background: conic-gradient(white, black);
</code></pre>
<p class="md">👆🏼Proposed in 2011, took until 2020 for [full support](https://caniuse.com/css-conic-gradients)</p>
<pre><code>
background: paint(conic-gradient, white black);
</code></pre>
<p class="md">👆🏼JS implementation can be written & shared immediately</p>
</div>
<details class="notes">
- [Current browser support](https://caniuse.com/css-paint-api)
- [Polyfill](https://github.com/GoogleChromeLabs/css-paint-polyfill)
</details>
</article>
<article class="slide language-css">
<h1>With fallback</h1>
<pre><code>
article {
border: 2px solid rebeccapurple;
}
@supports (border-image: paint(foo)) {
article {
border-image: paint(fancy-border);
}
}
</code></pre>
</article>
<article class="slide">
<h1>How does it work?</h1>
<pre><code>
// Main JS:
CSS.paintWorklet.addModule("paint-conic-gradient.js");
// paint-conic-gradient.js:
registerPaint("conic-gradient", class {
static get inputArguments() { return ["<color>+"]; }
paint(ctx, size, properties, args) {
// Draw conic gradient on ctx using Canvas methods
// args contains the paint() arguments (the colors)
}
});
</code></pre>
<details class="notes">
A [full conic gradient implementation with Paint API](https://css-houdini.rocks/conic-gradient/)
</details>
</article>
<article class="slide">
<h1>Self-contained Paint Worklet</h1>
<pre><code>
if (typeof PaintWorkletGlobalScope === "undefined") {
// We are not in a paint worklet, register our worklet
CSS.paintWorklet.addModule(import.meta.url);
}
else {
// We are in a paint worklet! Let’s paint! 🎨
registerPaint("conic-gradient", class {
static get inputArguments() { return ["<color>+"]; }
paint(ctx, size, properties, args) {
// Draw conic gradient on ctx
}
});
}
</code></pre>
</article>
<article class="confused slide language-css">
<h1>The reality…</h1>
<div><pre><code>paint(conic-gradient, white, black)</code></pre> doesn't work. No arguments yet.</div>
</article>
<article class="slide language-css">
<h1>Custom properties as input</h1>
<div>
<p>Instead of:</p>
<pre><code>
background: paint(conic-gradient, white black);
</code></pre>
<p>This works today:</p>
<pre><code>
--conic-gradient-colors: white black;
background: paint(conic-gradient);
</code></pre>
</div>
</article>
<article class="slide">
<pre><code>
CSS.registerProperty({
name: "--conic-gradient-colors",
syntax: "<color>+",
initialValue: "transparent",
inherits: false
});
registerPaint("conic-gradient", class {
static get inputProperties() {
return ["--conic-gradient-colors"];
}
paint(ctx, size, properties) {
let colors = properties.get("--conic-gradient-colors");
// Draw conic gradient on ctx
}
});
</code></pre>
</article>
<article class="question slide">
<h1>Why is that a problem?</h1>
</article>
<article class="slide language-css">
<h1>How to rewrite this?</h1>
<pre><code>
background: paint(conic-gradient, white silver);
border-image: paint(conic-gradient, white black);
list-style-image: paint(conic-gradient, lime yellow);
mask-image: paint(conic-gradient, transparent black);
</code></pre>
<div class="delayed">
<p>Or even this…</p>
<pre><code>
background: paint(conic-gradient, white transparent),
paint(conic-gradient, lime yellow);
</code></pre>
</div>
<details class="notes">
Admittedly, this is an extreme example to illustrate the problem.
While it is highly unlikely that you may want to use that many conic gradients
on a single element, it's far more likely with other types of paint worklets,
e.g. those drawing shapes.
As a side point, notice that these images are not just useful as backgrounds,
but there are many more image-accepting properties!
</details>
</article>
<article class="takeaway slide">
<h1>Custom properties apply to the element,
arguments apply to the image</h1>
</article>
<article class="slide language-css">
<h1>What about this?</h1>
<pre><code>
@property --background-image {
syntax: "<image> | none";
initial-value: none;
inherits: false;
}
.my-div {
--background-image: url(img/cat1.jpg);
--background-opacity: .5;
background: paint(background-opacity);
}
</code></pre>
</article>
<article class="confused slide">
<h1>Nope.</h1>
<p>Registering a custom property as <code><image></code><br> is not yet supported. 👎🏼</p>
<p class="md delayed">…although there are some [terrible hacks](https://css-houdini.rocks/background-properties)</p>
<details class="notes language-css">
This demo uses `list-style-image` (an image property that is not used)
to stuff a custom property in and actually get an image back
(i.e. a `CSSImageValue` in JS that can be painted on to canvas)
</details>
</article>
<header class="slide" style="--icon: 💩; background-color: var(--accent3)">
<h1>What about this?</h1>
</header>
<article class="slide language-css">
<h1>Idea: CSS API</h1>
<pre><code>
--background-icon: 💩;
background: paint(emoji-polka);
</code></pre>
</article>
<article class="slide">
<h1>Idea: JS implementation</h1>
<pre><code>
registerPaint("emoji-polka", class {
static get inputProperties() {
return ["--background-icon"];
}
paint(ctx, size, properties) {
let icon = properties.get("--background-icon");
ctx.fillText(icon, 0, 0);
}
});
</code></pre>
<details class="notes">
Obviously we'd eventually refine it to set the font size,
fine-tune the emoji position etc
</details>
</article>
<article class="confused slide">
<h1>Nope.</h1>
<p>No text rendering methods on <code>ctx</code> 👎🏼</p>
</article>
<article class="slide">
<blockquote style="width: 21em">
<div class="md">
**Note:** The `PaintRenderingContext2D` implements a subset of the `CanvasRenderingContext2D` API.
Specifically it doesn’t implement the `CanvasImageData`, `CanvasUserInterface`, `CanvasText`, or `CanvasTextDrawingStyles` APIs.
</div>
<p class="cite"><a href="https://drafts.css-houdini.org/css-paint-api-1/#2d-rendering-context">CSS Painting API Level 1</a></p>
</blockquote>
</article>
<article class="question slide">
<h1>So what can Paint API do today?</h1>
</article>
<article class="slide">
<h1 class="md">
[Lots of things!](https://houdini.how)<br>
[And more!](https://css-houdini.rocks)
</h1>
</article>
</section>
<footer class="dark slide dont-resize">
<img src="img/mark.svg" class="logo" />
<h1>Thank you!</h1>
<img src="img/stickers.jpg" class="stickers delayed" alt="">
<p class="stickercaption delayed">Want one? Just ask! ---></p>
<ul>
<li style="--icon: '🌍'"><a href="http://mavo.io">lea.verou.me</a></li>
<li style="--icon: '✉'"><a href="mailto:[email protected]">[email protected]</a></li>
<li style="--icon: '🐦'"><a href="http://twitter.com/leaverou">@leaverou</a></li>
</ul>
<p>Slides: <a href="https://leaverou.github.io/talks/css-variables">leaverou.github.io/talks/css-variables</a></p>
<p style="font-size: 50%;">This slide deck was entirely built with open web technologies! Slideshow framework: <a href="http://github.com/LeaVerou/CSSS">github.com/LeaVerou/CSSS</a></p>
</footer>
<script src="https://projects.verou.me/talks/shared/js/bliss.shy.min.js"></script>
<script src="https://projects.verou.me/talks/shared/js/prism.js"></script>
<script src="https://projects.verou.me/incrementable/incrementable.js"></script>
<script src="https://inspirejs.org/inspire.js"></script>
<!-- <script src="cssvars-js.js"></script> -->
<script src="talk.js"></script>
</body>
</html>