Skip to content

Instantly share code, notes, and snippets.

@kohyama
Last active March 3, 2018 20:44
Show Gist options
  • Save kohyama/6192253 to your computer and use it in GitHub Desktop.
Save kohyama/6192253 to your computer and use it in GitHub Desktop.
Example to operate DOM in ClojureScript via Browser REPL
(.log js/console "Hello JavaScript Console!")
(js/alert "Hello JavaScript in a page!")
(ns foo (:require [clojure.browser.dom :as dom]))
(def pane (dom/element :div))
(dom/append (.-body js/document) pane)
(def ta (dom/element :textarea {:cols 80 :rows 10}))
(dom/append pane ta)
(dom/set-value ta "Hello textarea from REPL!")
(dom/set-value ta "Hello textarea from REPL!\nYou can operate DOM elements from your REPL")
(dom/append pane (dom/element :br))
(dom/append pane (dom/element :input {:type "text" :value "One more example"}))
(set! (.-id pane) "pane")
(dom/remove-children "pane")
(def cvs (dom/element :canvas {:width 100 :height 100}))
(set! (.-border (.-style cvs)) "dotted 3px blue"))
(dom/append pane cvs)
(set! (.-border (.-style cvs)) "solid 1px black"))
(set! (.-width cvs) 320)
(set! (.-height cvs) 240)
(def ctx (.getContext cvs "2d"))
(set! (.-fillStyle ctx) "rgb(255,0,0)")
(.beginPath ctx)
(.arc ctx 160 120 72 0.0 (* 2.0 Math/PI) false)
(.fill ctx)
:cljs/quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment