-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
151 lines (108 loc) · 2.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
$(document).ready(function() {
var width = 1;
$(function() {
$( "#slider" ).slider({
value: width,
min:1,
max:50,
orientation: "horizontal",
range: "min",
animate: true,
slide: function( event, ui ) {
width = $( "#slider" ).slider( "value" );
console.log("w"+width);
}
});
});
var strokeColor = "black";
$('.colorbox').on('click', function() {
strokeColor = $(this).data("bcolor");
});
function findPos(obj) {
var curleft = 0, curtop = 0;
if (obj.offsetParent) {
do {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);
return { x: curleft, y: curtop };
}
return undefined;
}
function getWidthSlider() {
width = $( "#slider" ).slider( "option", "value" );
return width;
}
var c=document.getElementById("DrawCanvas");
var ctx=c.getContext("2d");
ctx.lineWidth=3;
var xCur;
var yCur;
var xStart;
var yStart;
var startNewLine = true;
$("#DrawCanvas").on("mousemove", function(e) {
ctx.lineWidth=getWidthSlider();
ctx.lineCap="round";
var pos = findPos(this);
var x = e.pageX - pos.x;
var y = e.pageY - pos.y;
if (startNewLine) {
xStart = x;
yStart = y;
}
ctx.beginPath();
ctx.strokeStyle=strokeColor;
if (e.which == 1) {
/* console.log('x: ' + e.pageX + ' y: '+ e.pageY); */
xEnd = x;
yEnd = y;
ctx.moveTo(xStart,yStart);
ctx.lineTo(xEnd,yEnd);
xStart = xEnd;
yStart = yEnd;
};
ctx.stroke();
ctx.save();
});
$('#undo').on('click', function() {
ctx.restore();
});
$(c).on("mouseup", function(){
startNewLine = true;
});
$(c).on("mousedown", function(){
startNewLine = false;
});
$(document).on('click', '#saveImage', function(c) {
/* var c = document.getElementById("sketch"); */
var dataString = c.toDataURL();
document.getElementById('canvasImg').src = dataString;
/* window.open(dataString); */
});
function to_image(){
document.getElementById("theimage").src = c.toDataURL();
}
function download_image(){
//Canvas2Image.saveAsPNG(canvas);
var image = c.toDataURL("image/png").replace("image/png", "image/octet-stream"); // here is the most important part because if you dont replace you will get a DOM 18 exception.
/* window.location.href=image; */
$data = base64_decode(image);
list($type, $data) = explode(';', $data);
list("", $data) = explode(',', $data);
$data = base64_decode($data);
/* file_put_contents('image.png', $data); */
var image = new Image();
image.src = $data;
document.body.appendChild(image);
}
/*
function save_image_local(){
var image = c.toDataURL("image/png").replace("image/png", "image/octet-stream");
$.ajax()
}
*/
document.getElementById('bt_draw').onclick = to_image
document.getElementById('bt_download').onclick = download_image
/* document.getElementById('bt_saveLocal').onclick = save_image_local */
});