|
| 1 | +$(function () |
| 2 | +{ |
| 3 | + //variables for function |
| 4 | + |
| 5 | + //paintingerasting or not |
| 6 | + var paint = false; |
| 7 | + |
| 8 | + //painting |
| 9 | + var paint_erase = 'paint'; |
| 10 | + |
| 11 | + //get canvas and context |
| 12 | + var canvas = document.getElementById('paint'); |
| 13 | + var ctx = canvas.getContext('2d'); |
| 14 | + |
| 15 | + //get the canvas container |
| 16 | + var container = $('#container'); |
| 17 | + |
| 18 | + //mouse position |
| 19 | + var mouse = {x: 0, y: 0}; |
| 20 | + |
| 21 | + //onload load saved |
| 22 | + if(localStorage.getItem('imgCanvas') != null) { |
| 23 | + var img = new Image(); |
| 24 | + img.onload = function () { |
| 25 | + ctx.drawImage(img, 0, 0); |
| 26 | + } |
| 27 | + img.src = localStorage.getItem('imgCanvas'); |
| 28 | + }; |
| 29 | + ctx.lineWidth = 3; |
| 30 | + ctx.linejoin = 'round'; |
| 31 | + ctx.lineCap = 'round'; |
| 32 | + //click inside button |
| 33 | + container.mousedown(function (e) { |
| 34 | + paint = true; |
| 35 | + ctx.beginPath(); |
| 36 | + mouse.x = e.pageX - this.offsetLeft; |
| 37 | + mouse.y = e.pageY - this.offsetTop; |
| 38 | + ctx.moveTo(mouse.x, mouse.y); |
| 39 | + }); |
| 40 | + //move the mouse |
| 41 | + container.mousemove(function (e) { |
| 42 | + mouse.x = e.pageX - this.offsetLeft; |
| 43 | + mouse.y = e.pageY - this.offsetTop; |
| 44 | + if (paint == true) { |
| 45 | + if (paint_erase == 'paint') { |
| 46 | + ctx.strokeStyle = $('#paintColor').val(); |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + ctx.strokeStyle = 'white'; |
| 51 | + } |
| 52 | + ctx.lineTo(mouse.x, mouse.y); |
| 53 | + ctx.stroke(); |
| 54 | + } |
| 55 | + }); |
| 56 | + //mouse up -> we are not paintingerasting anymore |
| 57 | + container.mouseup(function () { |
| 58 | + paint = false; |
| 59 | + }); |
| 60 | + container.mouseleave(function () { |
| 61 | + paint = false; |
| 62 | + }); |
| 63 | + ////////////////////////////////////// |
| 64 | + $('#reset').click(function () { |
| 65 | + ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 66 | + paint_erase = 'paint'; |
| 67 | + $('#erase').removeClass('eraseMode'); |
| 68 | + }); |
| 69 | + $('#save').click(function () { |
| 70 | + if (typeof (localStorage) != null) { |
| 71 | + localStorage.setItem('imgCanvas', canvas.toDataURL()); |
| 72 | + } |
| 73 | + else{ |
| 74 | + window.alert('Your browser does not support localstorage!'); |
| 75 | + } |
| 76 | + }); |
| 77 | + $('#erase').click(function () { |
| 78 | + if (paint_erase == 'paint') { |
| 79 | + paint_erase = 'erase'; |
| 80 | + } |
| 81 | + else{ |
| 82 | + paint_erase = 'paint'; |
| 83 | + } |
| 84 | + $(this).toggleClass('eraseMode'); |
| 85 | + }); |
| 86 | + // change color input |
| 87 | + $('#paintColor').change(function () { |
| 88 | + $('#circle').css('background-color',$(this).val()); |
| 89 | + }); |
| 90 | + $('#slider').slider({ |
| 91 | + min: 3, |
| 92 | + max: 30, |
| 93 | + slide:function(event,ui) { |
| 94 | + $('#circle').height(ui.value), |
| 95 | + $('#circle').width(ui.value); |
| 96 | + ctx.lineWidth = ui.value; |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | +}); |
0 commit comments