Skip to content

Commit bb7841e

Browse files
committed
[css-paint-api] Add arcs example.
Going to use a simpler arcs example instead of the conic-gradient. Also partially addresses w3c#340.
1 parent e6d69a2 commit bb7841e

File tree

3 files changed

+169
-7
lines changed

3 files changed

+169
-7
lines changed

css-paint-api/Overview.bs

+87-7
Original file line numberDiff line numberDiff line change
@@ -684,16 +684,16 @@ It is possible for an author to use paint to draw a placeholder image while an i
684684
loaded.
685685

686686
<pre class='lang-markup'>
687-
&lt;div id="myElement">
688-
&lt;/div>
689-
687+
&lt;!DOCTYPE html>
690688
&lt;style>
691-
#myElement {
689+
#example {
692690
--image: url('#someUrlWhichIsLoading');
693691
background-image: paint(image-with-placeholder);
694692
}
695693
&lt;/style>
696694

695+
&lt;div id="example">&lt;/div>
696+
697697
&lt;script>
698698
CSS.registerProperty({
699699
name: '--image',
@@ -729,10 +729,90 @@ registerPaint('image-with-placeholder', class {
729729
});
730730
</pre>
731731

732-
Example 3: Conic-gradient {#example-3}
733-
--------------------------------------
732+
Example 3: Arcs {#example-3}
733+
----------------------------
734+
735+
<pre class='lang-markup'>
736+
&lt;!DOCTYPE html>
737+
&lt;style>
738+
#example {
739+
width: 200px;
740+
height: 200px;
741+
742+
background-image:
743+
paint(arc, purple, 0.4turn, 0.8turn, 40px, 15px),
744+
paint(arc, blue, -20deg, 170deg, 30px, 20px),
745+
paint(arc, red, 45deg, 220deg, 50px, 10px);
746+
}
747+
&lt;/style>
748+
749+
&lt;div id="example">&lt;/div>
750+
751+
&lt;script>
752+
CSS.paintWorklet.import('arc.js');
753+
&lt;/script>
754+
</pre>
755+
756+
<pre class='lang-javascript'>
757+
// arc.js
758+
registerPaint('arc', class {
759+
static get inputArguments() {
760+
return [
761+
'&lt;color>',
762+
'&lt;angle>', // startAngle
763+
'&lt;angle>', // endAngle
764+
'&lt;length>', // radius
765+
'&lt;length>', // lineWidth
766+
];
767+
}
768+
769+
paint(ctx, geom, _, args) {
770+
ctx.strokeStyle = args[0].cssText;
771+
772+
// Determine the center point.
773+
const x = geom.width / 2;
774+
const y = geom.height / 2;
775+
776+
// Convert the start and end angles to radians.
777+
const startAngle = this.convertAngle(args[1]) - Math.PI / 2;
778+
const endAngle = this.convertAngle(args[2]) - Math.PI / 2;
779+
780+
// Convert the radius and lineWidth to px.
781+
const radius = this.convertLength(args[3]);
782+
const lineWidth = this.convertLength(args[4]);
783+
784+
ctx.lineWidth = lineWidth;
785+
786+
ctx.beginPath();
787+
ctx.arc(x, y, radius, startAngle, endAngle, false);
788+
ctx.stroke();
789+
}
790+
791+
convertAngle(angle) {
792+
switch (angle.unit) {
793+
case 'deg':
794+
return angle.value * Math.PI / 180;
795+
case 'rad':
796+
return angle.value;
797+
case 'grad':
798+
return angle.value * Math.PI / 200;
799+
case 'turn':
800+
return angle.value * Math.PI / 0.5;
801+
default:
802+
throw Error(`Unknown angle unit: ${angle.unit}`);
803+
}
804+
}
734805

735-
Issue: Add conic-gradient as a use case once we have function arguments.
806+
convertLength(length) {
807+
switch (length.type) {
808+
case 'px':
809+
return length.value;
810+
default:
811+
throw Error(`Unkown length type: ${length.type}`);
812+
}
813+
}
814+
});
815+
</pre>
736816

737817
Example 4: Different color based on size {#example-4}
738818
-----------------------------------------------------

css-paint-api/arc/arc.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
registerPaint('arc', class {
2+
static get inputArguments() {
3+
return [
4+
'<color>',
5+
'<angle>', // startAngle
6+
'<angle>', // endAngle
7+
'<length>', // radius
8+
'<length>', // lineWidth
9+
];
10+
}
11+
12+
constructor() {
13+
this.regex = /[a-z]+/g;
14+
}
15+
16+
paint(ctx, geom, _, args) {
17+
ctx.strokeStyle = args[0].cssText;
18+
19+
// Determine the center point.
20+
const x = geom.width / 2;
21+
const y = geom.height / 2;
22+
23+
// Convert the start and end angles to radians.
24+
const startAngle = this.convertAngle(args[1]) - Math.PI / 2;
25+
const endAngle = this.convertAngle(args[2]) - Math.PI / 2;
26+
27+
// Convert the radius and lineWidth to px.
28+
const radius = this.convertLength(args[3]);
29+
const lineWidth = this.convertLength(args[4]);
30+
31+
ctx.lineWidth = lineWidth;
32+
33+
ctx.beginPath();
34+
ctx.arc(x, y, radius, startAngle, endAngle, false);
35+
ctx.stroke();
36+
}
37+
38+
convertAngle(angle) {
39+
const value = angle.value || convertFloat(angle.cssText);
40+
const unit = angle.unit || angle.cssText.match(this.regex)[0];
41+
42+
switch (unit) {
43+
case 'deg':
44+
return value * Math.PI / 180;
45+
case 'rad':
46+
return value;
47+
case 'grad':
48+
return value * Math.PI / 200;
49+
case 'turn':
50+
return value * Math.PI / 0.5;
51+
default:
52+
throw Error(`Unknown angle unit: ${unit}`);
53+
}
54+
}
55+
56+
convertLength(length) {
57+
switch (length.type) {
58+
case 'px':
59+
return length.value;
60+
default:
61+
throw Error(`Unkown length type: ${length.type}`);
62+
}
63+
}
64+
});

css-paint-api/arc/index.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<style>
3+
#example {
4+
width: 200px;
5+
height: 200px;
6+
7+
background-image:
8+
paint(arc, purple, 0.4turn, 0.8turn, 40px, 15px),
9+
paint(arc, blue, -20deg, 170deg, 30px, 20px),
10+
paint(arc, red, 45deg, 220deg, 50px, 10px);
11+
}
12+
</style>
13+
14+
<div id="example"></div>
15+
16+
<script>
17+
(CSS.paintWorklet || paintWorklet).import('arc.js');
18+
</script>

0 commit comments

Comments
 (0)