-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.js
118 lines (97 loc) · 2.83 KB
/
color.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
var imageURL;
var n = 0;
var canvas;
var currentImg;
var brushColor = "#1abc9c";
var brushSize = 10;
var c01;
var images;
window.onload = function(){
scaleCanvas();
$.ajax({url:'images.json',success:function(data){
images = data.images;
//console.log(images);
$.each(data.images, function(i,image){
$('#image-select').append( new Option(image.title,i) );
});
loadImage(0);
},
});
canvas = new fabric.Canvas('myCanvas');
c01 = document.getElementById('c01');
var paletteWidth = window.getComputedStyle(c01,null).getPropertyValue("width");
var colorbox = document.getElementsByClassName('colorbox');
var getBgColor = function() {
brushColor = window.getComputedStyle(this,null).getPropertyValue("background-color");
for (var i = 0; i < colorbox.length; i++) {
$(colorbox[i]).removeClass('selectedColor').addClass('unselectedColor');
}
$(this).removeClass('unselectedColor').addClass('selectedColor');
//console.log(brushColor);
updateBrushColor();
};
for (var i = 0; i < colorbox.length; i++) {
colorbox[i].addEventListener('click', getBgColor, false);
}
$('#brushSize input:radio').addClass('input_hidden');
$('#brushSize label').click(function(){
$(this).addClass('selected').siblings().removeClass('selected');
});
canvas.isDrawingMode = true;
canvas.freeDrawingBrush.color = brushColor;
canvas.freeDrawingBrush.width = brushSize;
fabric.Image.fromURL(imageURL, function(oImg){
canvas.calcOffset();
var yScale = canvas.height/oImg.height;
var xScale = canvas.width/oImg.width;
oImg.scale(Math.min(yScale,xScale));
currentImg = oImg;
drawOverlay();
});
canvas.on('mouse:up', function(options) {
n++;
drawOverlay();
});
};
function drawOverlay(){
canvas.remove(currentImg);
canvas.add(currentImg);
canvas.renderAll();
};
function OnChange(imageIndex){
loadImage(imageIndex);
};
function loadImage(imageIndex){
canvas.clear().renderAll();
imageURL = images[imageIndex].url;
$('#artName').html(images[imageIndex].artworkName);
$('#artist').html(images[imageIndex].artist);
$('#artUrl').attr('href',images[imageIndex].artworkUrl);
$('#artLicence').html(images[imageIndex].license);
fabric.Image.fromURL(imageURL, function(oImg){
canvas.calcOffset();
var yScale = canvas.height/oImg.height;
var xScale = canvas.width/oImg.width;
oImg.scale(Math.min(yScale,xScale));
currentImg = oImg;
drawOverlay();
});
}
function scaleCanvas(){
var canvas = document.getElementById("myCanvas");
canvas.width = document.getElementById("canvas_cont").offsetWidth;
canvas.height = canvas.width * 0.8;
};
function updateBrushColor(){
canvas.freeDrawingBrush.color = brushColor;
};
function setBrushSize(size){
if (size=="large"){
brushSize = 20;
} else if (size=="medium"){
brushSize = 10;
}else {
brushSize = 5;
}
canvas.freeDrawingBrush.width = brushSize;
};