-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbadge.html
168 lines (145 loc) · 6.65 KB
/
badge.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Badger, badger, badger, mushroom, mushroom!</title>
<script type='text/javascript' src='jquery-2.1.4.min.js'></script>
<style type='text/css'>
.draggable { cursor: move; } /* Change mouse cursor */
@media print { .no-print, .no-print * { display: none !important; } }
</style>
<script type='text/javascript'>//<![CDATA[
function setEdit(elm) {
$("#txtItem").val( elm.id );
$("#txtText").val( elm.innerHTML );
$("#txtFont").val( $(elm).css('font-family') );
$("#txtSize").val( $(elm).css('font-size') );
$("#txtColor").val( rgbToHex($(elm).css('fill')) );
}
function save() {
// hack til at vælge det valgte tekstfeltet i svg billedet:
var elm = $("#" + $("#txtItem").val() )[0];
elm.innerHTML = $("#txtText").val(); // tekst
$(elm).css('font-family', $("#txtFont").val()); // font
//elm.style['font-size'] = $("#txtSize").val();
$(elm).css('font-size', $("#txtSize").val()); // størrelse
$(elm).css('fill', $("#txtColor").val()); // farve
}
function dropHandler(file) {
file.stopPropagation(); // don't change webpage to image file
var localFilename = file.dataTransfer.files[0].name;
var BgAndFg = [$('#bgImg')[0],$('#fgImg')[0]];
if ($.inArray(file.target, BgAndFg) != -1) {
// change bagground
$('#bgImg')[0].href.baseVal = localFilename;
$('#fgImg')[0].href.baseVal = localFilename;
/*} else if (file.target == $('#addImg')[0]) {
var img = $(new Image());
img.attr('class', 'draggable');
img.attr('onmousedown', 'selectElement(event)');
img.attr('ondragover', 'event.preventDefault()');
img.attr('src', '' + localFilename).appendTo($(file.target)).fadeIn();
*/
} else {
file.target.href = localFilename;
//console.log("change image " + file.target + " to image " + localFilename);
}
//file.dataTransfer.files[0] // bemærk problemer med Chrome Dev Console: http://stackoverflow.com/a/11573873
}
function getSelectedElements(evt) {
var BgAndFg = [$('#bgImg')[0],$('#fgImg')[0]];
if ($.inArray(evt.target, BgAndFg) != -1) {
// user has selected front- or background, so return both!
return BgAndFg;
}
return [evt.target]; // return list of one element
}
// Modificeret kode fra http://www.petercollingridge.co.uk/interactive-svg-components/draggable-svg-element
var selectedElements = [];
var currentX = 0, currentY = 0;
function selectElement(evt) {
selectedElements = [evt.target];
// Special case, if user selects front- or background then make sure to select both:
var fgAndBg = [ $('#fgImg')[0], $('#bgImg')[0] ];
if ( $.inArray(evt.target, fgAndBg) != -1) {
selectedElements = fgAndBg;
}
currentX = evt.clientX;
currentY = evt.clientY;
selectedElements.forEach(function(selectedElement) {
selectedElement.setAttributeNS(null, "onmousemove", "moveElement(evt)");
selectedElement.setAttributeNS(null, "onmouseout", "deselectElement(evt)");
selectedElement.setAttributeNS(null, "onmouseup", "deselectElement(evt)");
});
}
function moveElement(evt) {
var dx = evt.clientX - currentX;
var dy = evt.clientY - currentY;
selectedElements.forEach(function(selectedElement) {
selectedElement.setAttributeNS(null, "x", parseInt(selectedElement.getAttributeNS(null, "x")) + dx );
selectedElement.setAttributeNS(null, "y", parseInt(selectedElement.getAttributeNS(null, "y")) + dy );
});
currentX = evt.clientX;
currentY = evt.clientY;
}
function deselectElement(evt) {
selectedElements.forEach(function(selectedElement) {
if (selectedElement != 0) {
selectedElement.removeAttributeNS(null, "onmousemove");
selectedElement.removeAttributeNS(null, "onmouseout");
selectedElement.removeAttributeNS(null, "onmouseup");
selectedElement = 0;
}
});
}
// HJÆLPE METODER
function componentToHex(c) {
var hex = parseInt(c).toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(rgb) {
// split from rbg(x, y,z) to [x, y, z]
rgb = rgb.replace(/[^\d,]/g, '').split(',');
// assign r = x, g = y, b = z
var r = rgb[0], g = rgb[1], b = rgb[2];
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
//]]>
</script>
</head>
<body ondragover="event.preventDefault();" ondrop="event.preventDefault(); dropHandler(event)">
<div style="width: 100%; overflow: hidden;">
<div style="float: left;">
<svg version="1.1" baseProfile="full" width="300" height="300" xmlns="http://www.w3.org/2000/svg">
<defs>
<clipPath id="cut-off-bottom">
<circle cx="150" cy="150" r="2.5cm" stroke="black" stroke-width="0.1mm" fill="transparent" />
</clipPath>
<path id="lower-path" d="M 60, 150 c 0, 120, 180, 120, 180, 0" stroke="black" fill="green" stroke-width="2" fill-opacity="0.5" />
<path id="upper-path" d="M 80, 150 c 0, -100, 140, -100, 140, 0" stroke="black" fill="green" stroke-width="2" fill-opacity="0.5" />
</defs>
<image id="fgImg" xlink:href="pic.JPG" x="-50" y="-50" height="400px" width="400px" clip-path="url(#cut-off-bottom)" />
<image id="bgImg" class="draggable" onmousedown="selectElement(event)" xlink:href="pic.JPG" x="-50" y="-50" height="400px" width="400px" style="opacity:0.2;" />
<circle cx="150" cy="150" r="3cm" stroke="black" stroke-width="0.1mm" stroke-dasharray="5,5" fill="transparent" />
<!-- Tekst -->
<g font-size="25" fill="white" text-anchor="middle">
<text>
<textPath xlink:href="#upper-path" startOffset="50%" id="TextTop" onclick="setEdit(this)">OPtur 2015</textPath>
<textPath xlink:href="#lower-path" startOffset="50%" id="TextBottom" onclick="setEdit(this)">C. Elektroteknologi</textPath>
</text>
</g>
<text x="150" y="175" font-size="60" text-anchor="middle" fill="white" id="TextMiddle" onclick="setEdit(this)">DTU</text>
</svg>
</div>
<div class='no-print'>
<input type="hidden" id="txtItem"><br>
Text: <input type="text" id="txtText"><br>
Font: <input type="text" id="txtFont"><br>
Size: <input type="text" id="txtSize"><br>
Color: <input type="color" id="txtColor" value="#ffffff"><br>
<button type="button" onclick="save();">Save</button><br>
<!--<div id="addImg" style="/*height: 50px; width: 50px; background-color: #000000*/">Drop image here to add.</div>-->
</div>
</div>
</body>
</html>