@@ -684,16 +684,16 @@ It is possible for an author to use paint to draw a placeholder image while an i
684
684
loaded.
685
685
686
686
<pre class='lang-markup'>
687
- <div id="myElement">
688
- </div>
689
-
687
+ <!DOCTYPE html>
690
688
<style>
691
- #myElement {
689
+ #example {
692
690
--image: url('#someUrlWhichIsLoading' );
693
691
background-image: paint(image-with-placeholder);
694
692
}
695
693
</style>
696
694
695
+ <div id="example"></div>
696
+
697
697
<script>
698
698
CSS.registerProperty({
699
699
name: '--image' ,
@@ -729,10 +729,90 @@ registerPaint('image-with-placeholder', class {
729
729
});
730
730
</pre>
731
731
732
- Example 3: Conic-gradient {#example-3}
733
- --------------------------------------
732
+ Example 3: Arcs {#example-3}
733
+ ----------------------------
734
+
735
+ <pre class='lang-markup'>
736
+ <!DOCTYPE html>
737
+ <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
+ </style>
748
+
749
+ <div id="example"></div>
750
+
751
+ <script>
752
+ CSS.paintWorklet.import('arc.js' );
753
+ </script>
754
+ </pre>
755
+
756
+ <pre class='lang-javascript'>
757
+ // arc.js
758
+ registerPaint('arc' , class {
759
+ static get inputArguments() {
760
+ return [
761
+ '<color>' ,
762
+ '<angle>' , // startAngle
763
+ '<angle>' , // endAngle
764
+ '<length>' , // radius
765
+ '<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
+ }
734
805
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>
736
816
737
817
Example 4: Different color based on size {#example-4}
738
818
-----------------------------------------------------
0 commit comments