Skip to content

Commit 80904d7

Browse files
committed
gradients
1 parent 42a5efd commit 80904d7

File tree

3 files changed

+92
-10
lines changed

3 files changed

+92
-10
lines changed

source/clog-canvas.lisp

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@
7979
(query obj "fillStyle"))
8080

8181
(defmethod (setf fill-style) (value (obj clog-context2d))
82-
(execute obj (format nil "fillStyle='~A'" value)))
82+
(execute obj (format nil "fillStyle=~A"
83+
(if (typep value 'clog-obj)
84+
(script-id value)
85+
(format nil "'~A'" value)))))
8386

8487
;;;;;;;;;;;;;;;;;;;
8588
;; canvas-filter ;;
@@ -448,12 +451,64 @@ https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/global
448451
:connection-id (clog::connection-id obj)
449452
:html-id web-id)))
450453

451-
;; createConicGradient
452-
;; need to add createLinearGradient
453-
;; need to add createRadialGradient
454-
;; need to add createPattern
454+
;;;;;;;;;;;;;;;;;;;;;;;;;;;
455+
;; create-conic-gradient ;;
456+
;;;;;;;;;;;;;;;;;;;;;;;;;;;
455457

456-
;; drawFocusIfNeeded
458+
(defgeneric create-conic-gradient (clog-context2d start-angle x y)
459+
(:documentation "Create conic gradient"))
460+
461+
(defmethod create-conic-gradient ((obj clog-context2d) start-angle x y)
462+
(let ((web-id (clog-connection:generate-id)))
463+
(js-execute obj (format nil "clog['~A']=~A.createConicGradient(~A,~A,~A)"
464+
web-id (script-id obj)
465+
start-angle x y))
466+
(make-instance 'clog-canvas-gradient
467+
:connection-id (clog::connection-id obj)
468+
:html-id web-id)))
469+
470+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
471+
;; create-linear-gradient ;;
472+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
473+
474+
(defgeneric create-linear-gradient (clog-context2d x0 y0 x1 y1)
475+
(:documentation "Create linear gradient"))
476+
477+
(defmethod create-linear-gradient ((obj clog-context2d) x0 y0 x1 y1)
478+
(let ((web-id (clog-connection:generate-id)))
479+
(js-execute obj (format nil "clog['~A']=~A.createLinearGradient(~A,~A,~A,~A)"
480+
web-id (script-id obj)
481+
x0 y0 x1 y1))
482+
(make-instance 'clog-canvas-gradient
483+
:connection-id (clog::connection-id obj)
484+
:html-id web-id)))
485+
486+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
487+
;; create-radial-gradient ;;
488+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
489+
490+
(defgeneric create-radial-gradient (clog-context2d x0 y0 r0 x1 y1 r1)
491+
(:documentation "Create radial gradient"))
492+
493+
(defmethod create-radial-gradient ((obj clog-context2d) x0 y0 r0 x1 y1 r1)
494+
(let ((web-id (clog-connection:generate-id)))
495+
(js-execute obj (format nil "clog['~A']=~A.createRadialGradient(~A,~A,~A,~A,~A,~A)"
496+
web-id (script-id obj)
497+
x0 y0 r0 x1 y1 r1))
498+
(make-instance 'clog-canvas-gradient
499+
:connection-id (clog::connection-id obj)
500+
:html-id web-id)))
501+
502+
;;;;;;;;;;;;;;;;;;;;
503+
;; create-pattern ;;
504+
;;;;;;;;;;;;;;;;;;;;
505+
506+
(defgeneric create-pattern (clog-context2d clog-obj repetition)
507+
(:Documentation "Create pattern"))
508+
509+
(defmethod create-pattern ((obj clog-context2d) clog-obj repetition)
510+
(execute obj (format nil "createPattern(~A,'~A')"
511+
(script-id clog-obj) repetition)))
457512

458513
;;;;;;;;;;;;;;;;
459514
;; draw-image ;;
@@ -807,6 +862,22 @@ https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/global
807862
(defmethod translate ((obj clog-context2d) x y)
808863
(execute obj (format nil "translate(~A,~A)" x y)))
809864

865+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
866+
;; Implementation - clog-canvas-gradient
867+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
868+
869+
(defclass clog-canvas-gradient (clog-obj)())
870+
871+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
872+
;; Methods - clog-canvas-gradient
873+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
874+
875+
(defgeneric add-color-stop (clog-canvas-gradient offset color)
876+
(:documentation "Add a color stop"))
877+
878+
(defmethod add-color-stop ((obj clog-canvas-gradient) offset color)
879+
(execute obj (format nil "addColorStop(~A,'~A')" offset color)))
880+
810881
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
811882
;; Implementation - clog-image-data
812883
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -919,9 +990,9 @@ https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/global
919990

920991
(defclass clog-path2d (clog-obj)())
921992

922-
;;;;;;;;;;;;;;;;;;;;;;
993+
;;;;;;;;;;;;;;;;;;;
923994
;; create-path2d ;;
924-
;;;;;;;;;;;;;;;;;;;;;;
995+
;;;;;;;;;;;;;;;;;;;
925996

926997
(defgeneric create-path2d (clog-canvas &key path2d)
927998
(:documentation "Create a new CLOG-Path2d. If CLOG-PATH2D creates a copy."))

source/clog.lisp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,10 @@ embedded in a native template application.)"
767767
(path-clip generic-function)
768768
(close-path generic-function)
769769
(create-image-data generic-function)
770+
(create-conic-gradient generic-function)
771+
(create-linear-gradient generic-function)
772+
(create-radial-gradient generic-function)
773+
(create-pattern generic-function)
770774
(draw-image generic-function)
771775
(draw-image-from-to generic-function)
772776
(ellipse generic-function)
@@ -796,6 +800,10 @@ embedded in a native template application.)"
796800
(transform generic-function)
797801
(translate generic-function)
798802

803+
"CLOG-Canvas-Gradient"
804+
(clog-canvas-gradien class)
805+
(add-color-stop generic-function)
806+
799807
"CLOG-Image-Data"
800808
(clog-image-data class)
801809
(json-image-data generic-function)

tutorial/10-tutorial.lisp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
;;; A very brief example of using the canvas control.
88
(defun on-new-window (body)
9-
(debug-mode body)
109
(setf (title (html-document body)) "Tutorial 10")
1110
(let* ((canvas (create-canvas body :width 600 :height 400))
1211
(cx (create-context2d canvas))
@@ -17,8 +16,12 @@
1716
(setf (fill-style cx) :blue
1817
(font-style cx) "bold 24px serif")
1918
(fill-text cx "Hello World" 10 150)
20-
(setf (fill-style cx) :red)
2119
(begin-path cx)
20+
(let ((gr (create-linear-gradient cx 20 0 220 0)))
21+
(add-color-stop gr 0 :red)
22+
(add-color-stop gr .5 :cyan)
23+
(add-color-stop gr 1 :yellow)
24+
(setf (fill-style cx) gr))
2225
(ellipse cx 200 200 50 7 0.78 0 6.29)
2326
(path-stroke cx)
2427
(path-fill cx)

0 commit comments

Comments
 (0)