Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
Basic clock function
  • Loading branch information
ethanf108 committed Aug 30, 2022
0 parents commit 5eedb57
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/out/**
1 change: 1 addition & 0 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:deps {org.clojure/clojurescript {:mvn/version "1.11.54"}}}
10 changes: 10 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<canvas id="draw" width="500" height="500"></canvas>
<script src="out/main.js" type="text/javascript"></script>
</body>
</html>
44 changes: 44 additions & 0 deletions src/weird_clock/main.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(ns weird-clock.main)


(def hours '(1 2 3 4 5 6 7 8 9 10 11 12))

(def draw-canvas (.getElementById js/document "draw"))

(def draw-width (.-width draw-canvas))
(def draw-height (.-height draw-canvas))
(def draw-ctx (.getContext draw-canvas "2d"))

(defn draw-line [ctx angle len]
(.beginPath ctx)
(.moveTo ctx (/ draw-width 2) (/ draw-height 2))
(.lineTo ctx
(+ (/ draw-width 2) (* (/ draw-width 2) (Math/cos angle) len))
(+ (/ draw-height 2) (* (/ draw-height 2) (Math/sin angle) len)))
(.stroke draw-ctx))


(defn draw-fn []
(set! (.-fillStyle draw-ctx) "rgb(0,255,255)")
(.fillRect draw-ctx 0 0 draw-width draw-height)
(set! (.-font draw-ctx) "18px sans-serif")
(set! (.-fontStyle draw-ctx) "bold")
(set! (.-fillStyle draw-ctx) "rgb(0,0,0)")
(loop
[index 11]
(when (>= index 0)
(.fillText draw-ctx
(nth hours index)
(+ (/ draw-width 2) (* (/ draw-width 2) (Math/cos (/ (* Math/PI index) 6)) 0.9))
(+ (/ draw-height 2) (* (/ draw-height 2) (Math/sin (/ (* Math/PI index) 6)) 0.9)))
(recur (- index 1))))
(set! (.-fillStyle draw-ctx) "rgb(255,0,0)")
(set! (.-lineWidth draw-ctx) 6)
(let [
time (/ (.now js/Date) 1000 60)
minute (mod time 60)
hour (- (mod (/ time 60) 12) (/ (.getTimezoneOffset (js/Date.)) 60))]
(draw-line draw-ctx (- (* Math/PI minute .0333333333) (/ Math/PI 2)) 0.9)
(draw-line draw-ctx (- (* Math/PI hour (/ 1 6)) (/ Math/PI 2)) 0.6)))

(js/setInterval draw-fn 1000)

0 comments on commit 5eedb57

Please sign in to comment.