<canvas id="canvas" class="dotmark ov" width="${bg_dim%x*}" height="${bg_dim#*x}"></canvas>
<input type=hidden id=image_serialize name=imagedata value="">
- <span id="jsdebug" style="display: none; position: fixed; right:0; bottom:0">Debug</span>
<button type="submit">$(l10n save)</button>
</form>
+<span id="jsdebug" style="display: none; position: fixed; right:0; bottom:0">Debug</span>
+
<script><!--
body = document.body
-canvas = document.getElementById("canvas")
dbg = document.getElementById("jsdebug")
+canvas = document.getElementById("canvas")
+data=document.getElementById("image_serialize")
+
image = canvas.getContext("2d")
mouse = 0
image_serialize=""
-data=document.getElementById("image_serialize")
+
+// start and current coordinates of a draw
+// serves for tracking, whether path ends close to its beginning
+stx=0, sty=0
+cux=0, cuy=0
function setcol(c) {
+ this.c = c
image.strokeStyle = c
+ image.fillStyle = c
if ( c == "#FFF" ) image.lineWidth = 32
else image.lineWidth = 4
- image_serialize += " stroke " + c + " stroke-width " + image.lineWidth
+ data.value += " stroke " + c + " stroke-width " + image.lineWidth
}
setcol("$tpy[color]")
+function relX(x){
+ if ( body.clientWidth >= 800 ){
+ return Math.floor(cscaleW * (x - canvas.offsetLeft))
+ } else {
+ return Math.floor(cscaleW * (x - canvas.offsetLeft + window.pageXOffset))
+ }
+}
+function relY(y){
+ if ( body.clientWidth >= 800 ){
+ return Math.floor(cscaleH * (y - canvas.offsetTop))
+ } else {
+ return Math.floor(cscaleH * (y - canvas.offsetTop + window.pageYOffset))
+ }
+}
+
function draw(x, y) {
if ( mouse == 1){
- if ( body.clientWidth >= 800 ){
- sx=Math.floor(cscaleW * (x - canvas.offsetLeft))
- sy=Math.floor(cscaleH * (y - canvas.offsetTop))
- } else {
- sx=Math.floor(cscaleW * (x - canvas.offsetLeft + window.pageXOffset))
- sy=Math.floor(cscaleH * (y - canvas.offsetTop + window.pageYOffset))
- }
- image.lineTo( sx, sy )
+ cux=relX(x), cuy=relY(y)
+
+ image.lineTo( cux, cuy )
image.stroke()
- image_serialize += " " + sx + "," + sy
+ image_serialize += " " + cux + "," + cuy
}
}
cscaleW = canvas.width / canvas.clientWidth
cscaleH = canvas.height / canvas.clientHeight
- image_serialize += " polyline"
+ stx=relX(x), sty=relY(y)
+
image.beginPath()
- draw(x, y)
+ draw(x, y) // why must this not use relative Coords ???
+
+ image_serialize = " polyline"
}
+
function drawstop() {
- mouse = 0
- image.closePath()
- //dbg.innerHTML = image_serialize
- data.value = image_serialize
+
+ // if path ends close to beginning ( < 50 px); then close path and fill
+ if ( mouse == 1 && Math.sqrt( Math.pow(stx - cux, 2) + Math.pow(sty - cuy, 2)) <= 50 ){
+ image.lineTo( stx, sty )
+ image.stroke()
+
+ image.globalAlpha = .5
+ image.fill()
+ image.globalAlpha = 1
+
+ image_serialize += " " + stx + "," + sty
+ data.value += " fill " + c + "8" + image_serialize
+ } else if (mouse == 1) {
+ data.value += " fill #0000 " + image_serialize
+ }
+ dbg.innerHTML = " stx: " + stx + " cux: " + cux + " sty: " + sty + " cuy: " + cuy
+
+ image.closePath()
+ image_serialize = ""
+ mouse = 0
}
window.addEventListener( 'mouseup', function() { drawstop() } )