Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
43 lines (34 sloc) 967 Bytes
package canvasapp
import org.scalajs.dom
import scalajs.js.annotation.JSExport
import dom.html
@JSExport
object ScratchPad extends{
@JSExport
def main(canvas: html.Canvas) = {
/*setup*/
val renderer = canvas.getContext("2d")
.asInstanceOf[dom.CanvasRenderingContext2D]
canvas.width = canvas.parentElement.clientWidth
canvas.height = canvas.parentElement.clientHeight
renderer.fillStyle = "#f8f8f8"
renderer.fillRect(0, 0, canvas.width, canvas.height)
/*code*/
renderer.fillStyle = "black"
var down = false
canvas.onmousedown =
(e: dom.MouseEvent) => down = true
canvas.onmouseup =
(e: dom.MouseEvent) => down = false
canvas.onmousemove = {
(e: dom.MouseEvent) =>
val rect =
canvas.getBoundingClientRect()
if (down) renderer.fillRect(
e.clientX - rect.left,
e.clientY - rect.top,
10, 10
)
}
}
}