-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.html
215 lines (189 loc) · 7.78 KB
/
index.html
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="css/index.css" />
<title>CanvasTools Samples - Images Editor</title>
</head>
<body>
<h1>CanvasTools Samples - Images Editor</h1>
<div id="canvasToolsDiv">
<div id="toolbarDiv">
</div>
<div id="selectionDiv">
<div id="editorDiv"></div>
</div>
</div>
<div id="controls">
<div>
<label for="imageSelect" >Choose image: </label>
<select id="imageSelect">
</select>
</div>
</div>
</body>
<script src="./../shared/js/ct.js"></script>
<script>
// The list of images
const images = [
{
path: "./../shared/media/background-cat-hd.jpg",
title: "Cat (HD)"
},
{
path: "./../shared/media/background-cat2.jpg",
title: "Cat - Grumpy"
},
{
path: "./../shared/media/background-cat3.jpg",
title: "Cat - Inspiring"
},
{
path: "./../shared/media/background-city.jpg",
title: "City"
},
{
path: "./../shared/media/background-forest-hd.jpg",
title: "Forest (HD)"
},
{
path: "./../shared/media/background-glass.jpg",
title: "Cafe"
},
{
path: "./../shared/media/background-sea-hd.jpg",
title: "Sea (HD)"
},
{
path: "./../shared/media/background-snow.jpg",
title: "Snow"
},
];
document.addEventListener("DOMContentLoaded", (e) => {
// Get references for editor and toolbar containers
const editorContainer = document.getElementById("editorDiv");
const toolbarContainer = document.getElementById("toolbarDiv");
// Init the editor with toolbar.
const editor = new CanvasTools.Editor(editorContainer).api;
editor.addToolbar(toolbarContainer, CanvasTools.Editor.FullToolbarSet, "./../shared/media/icons/");
let incrementalRegionID = 100;
// Add new region to the editor when new region is created
editor.onSelectionEnd = (regionData) => {
let id = (incrementalRegionID++).toString();
// Generate random tag
let tags = generateRandomTagsDescriptor();
editor.addRegion(id, regionData, tags);
console.log(`Created ${id}: {${regionData.x}, ${regionData.y}} x {${regionData.width}, ${regionData.height}}`);
};
// Log region manipulations
editor.onRegionMoveBegin = (id, regionData) => {
console.log(`Began moving ${id}: {${regionData.x}, ${regionData.y}} x {${regionData.width}, ${regionData.height}}`);
};
editor.onRegionMove = (id, regionData) => {
// console.log(`Moving ${id}: {${regionData.x}, ${regionData.y}} x {${regionData.width}, ${regionData.height}}`);
};
editor.onRegionMoveEnd = (id, regionData) => {
console.log(`Ended moving ${id}: {${regionData.x}, ${regionData.y}} x {${regionData.width}, ${regionData.height}}`);
};
editor.onRegionSelected = (id, multiselection) => {
console.log(`Selected ${id}: multiselection = ${multiselection}`);
}
editor.onRegionDelete = (id, regionData) => {
console.log(`Deleted ${id}: {${regionData.x}, ${regionData.y}} x {${regionData.width}, ${regionData.height}}`);
};
// Short references to Colors.* classes
const Color = CanvasTools.Core.Colors.Color;
const RGBColor = CanvasTools.Core.Colors.RGBColor;
const LABColor = CanvasTools.Core.Colors.LABColor;
const HSLColor = CanvasTools.Core.Colors.HSLColor;
// Collection of primary tags. Use Color class to define color.
const primaryTags = [
new CanvasTools.Core.Tag("Awesome", new Color("#c48de7")),
new CanvasTools.Core.Tag("Amazing", new Color("#3b1")),
new CanvasTools.Core.Tag("Brilliante", new Color("#f94c48")),
];
// Collection of secondary tags. Use hex-string to define color.
const secondaryTags = [
new CanvasTools.Core.Tag("Yes", "#fff"),
new CanvasTools.Core.Tag("No", "#000"),
new CanvasTools.Core.Tag("Unknown", "#999"),
];
// Collection of ternary tags. Use varios color formats to define color.
const ternaryTags = [
new CanvasTools.Core.Tag("one", new Color(new RGBColor(0.55, 0.24, 0.25))),
new CanvasTools.Core.Tag("two", new Color(new HSLColor(0.32, 0.27, 0.51))),
new CanvasTools.Core.Tag("many", new Color(new LABColor(0.62, 0.50, -0.55))),
]
// Randomly generate tags descriptor object
function generateRandomTagsDescriptor() {
const rnd = (n) => {
let r = Math.floor(Math.random() * n);
if (r === n) {
r = n - 1;
}
return r;
};
const primaryTag = primaryTags[rnd(3)];
const secondaryTag = secondaryTags[rnd(3)];
const ternaryTag = ternaryTags[rnd(3)];
const r = Math.random();
let tags;
if (r < 0.2) {
// Create tags descriptor passing all three tags
tags = new CanvasTools.Core.TagsDescriptor(primaryTag, [secondaryTag, ternaryTag]);
} else if (requestAnimationFrame < 0.4) {
// Create tags descriptor without primary tag
tags = new CanvasTools.Core.TagsDescriptor(null, [secondaryTag, ternaryTag]);
} else if (r < 0.6) {
// Create tags descriptor defining only primary tags
tags = new CanvasTools.Core.TagsDescriptor(primaryTag);
} else if (r < 0.8) {
// Create tags descriptor passing array of tags
tags = new CanvasTools.Core.TagsDescriptor([primaryTag, secondaryTag]);
} else {
// Create tags descriptor without specifying tags
tags = new CanvasTools.Core.TagsDescriptor();
}
return tags;
};
let imageIndex = 0;
// Init images selector
initImageSelect(images, (index, path) => {
imageIndex = index;
loadImage(path, (image) => {
editor.addContentSource(image);
});
// Delete current regions on image change
editor.deleteAllRegions();
});
// Load first image
loadImage(images[imageIndex].path, (image) => {
editor.addContentSource(image);
});
});
// Builds select element using provided list of images
function initImageSelect(images, onSelect) {
var imageSelect = document.getElementById("imageSelect");
images.forEach((image) => {
let o = document.createElement("option");
o.text = image.title;
imageSelect.add(o);
})
imageSelect.selectedIndex = 0;
// Register listener for image change
imageSelect.addEventListener("change", (e) => {
let index = imageSelect.selectedIndex;
if (index >= 0 && index < images.length) {
onSelect(index, images[index].path);
}
});
}
// Load an image from specified path and notify when it is loaded.
function loadImage(path, onready) {
const image = new Image();
image.addEventListener("load", (e) => {
onready(e.target);
});
image.src = path;
}
</script>
</html>