I am quite new to clojurescript and maybe this is a trivial question but I did not manage to find the an answer yet.
I am looking forward to implementing a clojurescript to track the mouse and render a dot at the mouse position, as implemented here:
https://jsbin.com/gejuz/1/edit?html,output
Js Code:
function() {
"use strict";
document.onmousemove = handleMouseMove;
function handleMouseMove(event) {
var dot, eventDoc, doc, body, pageX, pageY;
event = event || window.event; // IE-ism
// If pageX/Y aren't available and clientX/Y
// are, calculate pageX/Y - logic taken from jQuery
// Calculate pageX/Y if missing and clientX/Y available
if (event.pageX == null && event.clientX != null) {
eventDoc = (event.target && event.target.ownerDocument) || document;
doc = eventDoc.documentElement;
body = eventDoc.body;
event.pageX = event.clientX +
(doc && doc.scrollLeft || body && body.scrollLeft || 0) -
(doc && doc.clientLeft || body && body.clientLeft || 0);
event.pageY = event.clientY +
(doc && doc.scrollTop || body && body.scrollTop || 0) -
(doc && doc.clientTop || body && body.clientTop || 0 );
}
// Add a dot to follow the cursor
dot = document.createElement('div');
dot.className = "dot";
dot.style.left = event.pageX + "px";
dot.style.top = event.pageY + "px";
document.body.appendChild(dot);
}
Up until now, I did manage to get the mouse coordinates (thanks to this question Tracking mouse in clojurescript / reagent / reagi?). But I am failing to render the dot in the webpage.
Clojurescript code:
(def mouse-coordinates (reagent/atom {:x 100 :y 100}))
(defn dot [x y]
[:div {:style {:left (str x "px")
:top (str y "px")
:width "2px"
:height "2px"
:background-clor "black"
:position "absolute"}}])
(def do-dot (reagent/reactify-component dot))
(defn mouse-move []
[:body
{:onMouseMove (fn [event]
(swap! mouse-coordinates assoc :x (.-clientX event))
(swap! mouse-coordinates assoc :y (.-clientY event))
(reagent/create-element do-dot
#js{:x (int (:x @mouse-coordinates))
:y (int (:y @mouse-coordinates))})
)}
[:p "x: " (int (:x @mouse-coordinates))]
[:p "y: " (int (:y @mouse-coordinates))]
])
(reagent/render-component [mouse-move]
(. js/document (getElementById "app")))
Any help is appreciated. Thank you in advance.