-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrawingtool.html
More file actions
165 lines (147 loc) · 6.36 KB
/
drawingtool.html
File metadata and controls
165 lines (147 loc) · 6.36 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<html>
<head>
<script src="https://unpkg.com/konva@9/konva.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<meta charset="utf-8" />
<title>Drawing Tool using Konva JS</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #f0f0f0;
}
.drawContainer{
display: flex;
align-items: center;
justify-content: center;
gap:5px;
padding: 5px;
margin-top: 5px;
}
#tool, #brush-color-picker {
height:20px;
}
</style>
</head>
<body>
<div class="drawContainer">
Drawing Tool:
<select id="tool">
<option value="brush">Brush</option>
<option value="eraser">Eraser</option>
</select>
<input type = "color" id ="brush-color-picker">
<input type="range" id="width-slider" min="0" max="100" step="1" value="5">
</div>
<div id="container"></div>
<script>
var brushsize = 5;
var color = '050505';
var mode = 'brush';
$('#brush-color-picker').change(function() {
context.strokeStyle = $(this).val();
var color = $(this).val();
if (mode === 'eraser') {
$('#container').css({
'cursor': 'url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\' fill=\'none\' stroke=\'%23000000\' width=\'' + brushsize + 'px\' height=\'' + brushsize + 'px\' viewBox=\'0 0 10.04 10.04\'><circle cx=\'5.02\' cy=\'5.02\' r=\'4.52\'/></svg>") ' + brushsize / 2 + ' ' + brushsize / 2 + ', auto',
});
} else if ((mode === 'brush')) {
$('#container').css({
'cursor': 'url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\' fill=\'%23' + color.substring(1) + '\' width=\'' + brushsize + 'px\' height=\'' + brushsize + 'px\' viewBox=\'0 0 10.04 10.04\'><circle cx=\'5.02\' cy=\'5.02\' r=\'4.52\'/></svg>") ' + brushsize / 2 + ' ' + brushsize / 2 + ', auto',
});
}
});
$('#width-slider').change(function() {
brushsize = $(this).val();
context.lineWidth = brushsize;
color = $('#brush-color-picker').val().substring(1);
if (mode === 'eraser') {
$('#container').css({
'cursor': 'url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\' fill=\'none\' stroke=\'%23000000\' width=\'' + brushsize + 'px\' height=\'' + brushsize + 'px\' viewBox=\'0 0 10.04 10.04\'><circle cx=\'5.02\' cy=\'5.02\' r=\'4.52\'/></svg>") ' + brushsize / 2 + ' ' + brushsize / 2 + ', auto',
});
} else if (mode === 'brush') {
$('#container').css({
'cursor': 'url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\' fill=\'%23' + color + '\' width=\'' + brushsize + 'px\' height=\'' + brushsize + 'px\' viewBox=\'0 0 10.04 10.04\'><circle cx=\'5.02\' cy=\'5.02\' r=\'4.52\'/></svg>") ' + brushsize / 2 + ' ' + brushsize / 2 + ', auto',
});
}
});
var width = window.innerWidth;
var height = window.innerHeight - 25;
var stage = new Konva.Stage({
container: 'container',
width: width,
height: height,
});
var layer = new Konva.Layer();
stage.add(layer);
var canvas = document.createElement('canvas');
canvas.width = stage.width();
canvas.height = stage.height();
var image = new Konva.Image({
image: canvas,
x: 0,
y: 0,
});
layer.add(image);
$('#container').css({
'cursor': 'url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\' fill=\'%23000000\' width=\'' + brushsize + 'px\' height=\'' + brushsize + 'px\' viewBox=\'0 0 10.04 10.04\'><circle cx=\'5.02\' cy=\'5.02\' r=\'4.52\'/></svg>") ' + (brushsize / 2) + ' ' + (brushsize / 2) + ', auto'
});
var context = canvas.getContext('2d');
context.strokeStyle = '#050505';
context.lineJoin = 'round';
context.lineWidth = 5;
var isPaint = false;
var lastPointerPosition;
var mode = 'brush';
image.on('mousedown touchstart', function () {
isPaint = true;
lastPointerPosition = stage.getPointerPosition();
});
stage.on('mouseup touchend', function () {
isPaint = false;
});
stage.on('mousemove touchmove', function () {
if (!isPaint) {
return;
}
if (mode === 'brush') {
context.globalCompositeOperation = 'source-over';
}
if (mode === 'eraser') {
context.globalCompositeOperation = 'destination-out';
}
context.beginPath();
var localPos = {
x: lastPointerPosition.x - image.x(),
y: lastPointerPosition.y - image.y(),
};
context.moveTo(localPos.x, localPos.y);
var pos = stage.getPointerPosition();
localPos = {
x: pos.x - image.x(),
y: pos.y - image.y(),
};
context.lineTo(localPos.x, localPos.y);
context.closePath();
context.stroke();
lastPointerPosition = pos;
layer.batchDraw();
});
var select = document.getElementById('tool');
select.addEventListener('change', function () {
mode = select.value;
if (mode === 'eraser') {
$('#container').css({
'cursor': 'url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\' fill=\'none\' stroke=\'%23000000\' width=\'' + brushsize + 'px\' height=\'' + brushsize + 'px\' viewBox=\'0 0 10.04 10.04\'><circle cx=\'5.02\' cy=\'5.02\' r=\'4.52\'/></svg>") ' + brushsize / 2 + ' ' + brushsize / 2 + ', auto',
});
} else if (mode === 'brush') {
var color = $('#brush-color-picker').val();
$('#container').css({
'cursor': 'url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\' fill=\'%23' + color.substring(1) + '\' width=\'' + brushsize + 'px\' height=\'' + brushsize + 'px\' viewBox=\'0 0 10.04 10.04\'><circle cx=\'5.02\' cy=\'5.02\' r=\'4.52\'/></svg>") ' + brushsize / 2 + ' ' + brushsize / 2 + ', auto',
});
}
});
</script>
</body>
</html>