-
Notifications
You must be signed in to change notification settings - Fork 1
/
membermugger.html
167 lines (161 loc) · 7.21 KB
/
membermugger.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
<!--
kwartzlab member mug generator, 2016 James Bastow <[email protected]>
This generates a PDF to be printed for the member photo wall
-->
<html>
<head>
<script src="lib/jquery-2.1.4.js"></script>
<script src="lib/jquery.Jcrop.js"></script>
<script src="lib/load-image.all.min.js"></script>
<script src="lib/pdfkit.js"></script>
<script src="lib/blob-stream.js"></script>
<script src="lib/FileSaver.js"></script>
<script src="generate-mug.js"></script>
<script>
// Font is in generate-mug.js as base64 data
var newStyle = document.createElement('style');
newStyle.appendChild(document.createTextNode("\
@font-face {\
font-family: 'Futura';\
src: url('" + FuturaLTBook + "');\
}\
"));
document.head.appendChild(newStyle);
$(function () {
'use strict';
// This is from the JavScript-Load-Image demo: https://blueimp.github.io/JavaScript-Load-Image/
var result = $('#result'),
thumbNode = $('#thumbnail'),
actionsNode = $('#actions'),
currentFile,
replaceResults = function (img) {
var content;
if (!(img.src || img instanceof HTMLCanvasElement)) {
content = $('<span>Loading image file failed</span>');
} else {
content = $('<a target="_blank">').append(img)
.attr('download', currentFile.name)
.attr('href', img.src || img.toDataURL());
}
result.children().replaceWith(content);
if (img.getContext) {
actionsNode.show();
}
},
displayImage = function (file, options) {
currentFile = file;
if (!loadImage(
file,
replaceResults,
options
)) {
result.children().replaceWith(
$('<span>Your browser does not support the URL or FileReader API.</span>')
);
}
},
dropChangeHandler = function (e) {
e.preventDefault();
e = e.originalEvent;
var target = e.dataTransfer || e.target,
file = target && target.files && target.files[0],
options = {
maxWidth: result.width(),
canvas: true
};
if (!file) {
return;
}
loadImage.parseMetaData(file, function (data) {
if (data.exif) {
options.orientation = data.exif.get('Orientation');
}
displayImage(file, options);
});
},
coordinates;
// Hide URL/FileReader API requirement message in capable browsers:
if (window.createObjectURL || window.URL || window.webkitURL || window.FileReader) {
//result.children().hide();
result.children().replaceWith($('<span>Drop photo here</span>'));
}
$(document)
.on('dragover', function (e) {
e.preventDefault();
e = e.originalEvent;
e.dataTransfer.dropEffect = 'copy';
})
.on('drop', dropChangeHandler);
$('#file-input').on('change', dropChangeHandler);
$('#edit').on('click', function (event) {
event.preventDefault();
var imgNode = result.find('img, canvas'),
img = imgNode[0];
imgNode.Jcrop({
aspectRatio: 2000/3008,
setSelect: [40, 40, img.width - 40, img.height - 40],
onSelect: function (coords) {
coordinates = coords;
},
onRelease: function () {
coordinates = null;
}
}).parent().on('click', function (event) {
event.preventDefault();
});
});
$('#crop').on('click', function (event) {
event.preventDefault();
var img = result.find('img, canvas')[0];
if (img && coordinates) {
replaceResults(loadImage.scale(img, {
left: coordinates.x,
top: coordinates.y,
sourceWidth: coordinates.w,
sourceHeight: coordinates.h,
minWidth: result.width()
}));
coordinates = null;
}
});
$('#save').on('click', function (event) {
// On newer browsers, this does not work when the page is served via file protocol due to CORS security measures
// There doesn't seem to be a workaround other than serving locally via http
var img = result.find('img, canvas')[0];
GenerateMug(img.toDataURL());
});
});
</script>
<link rel="stylesheet" href="lib/jquery.Jcrop.css" type="text/css" />
<style>
body { font-family: 'Futura'; }
.result {
padding: 20px;
background: #fff;
color: #222;
text-align: center;
border-style: solid;
border-width: 1px;
margin: auto;
width: 400px;
height: 600px;
}
.jcrop-holder {
margin: 0 auto;
}
</style>
</head>
<body>
<h1 style="text-align: center;">kwartzlab member mug generator</h1>
<div id="result" class="result">
<p>This demo works only in browsers with support for the <a href="https://developer.mozilla.org/en/DOM/window.URL">URL</a> or <a href="https://developer.mozilla.org/en/DOM/FileReader">FileReader</a> API.</p>
</div>
<p id="actions" style="display:none; text-align:center;">
<input type="text" id="name" value="Name"/>
<br />
<button type="button" id="edit">Edit</button>
<button type="button" id="crop">Crop</button>
<button type="button" id="save">Save</button>
</p>
</body>
</html>