-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
183 lines (138 loc) · 4.99 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
<html>
<head>
<meta name="viewport" content="initial-scale = 1.0,maximum-scale = 1.0">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="wrapper">
<div id="p1" class="draggable">1</div>
<div id="p2" class="draggable">2</div>
<div id="p3" class="draggable">3</div>
<div id="p4" class="draggable">4</div>
<img id="image" src="https://i.stack.imgur.com/RvM2m.jpg">
<svg id="svg">
<defs>
<clipPath id="fogOfWar">
<polygon id="poly" points="" />
</clipPath>
</defs>
</svg>
</div>
<form>
<input type="submit" value="Submit">
</form>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script>
var scaling_factor_w;
var scaling_factor_h;
var origW;
var origH;
$(function(){
if (window.safari) {
history.pushState(null, null, location.href);
window.onpopstate = function(event) {
history.go(1);
};
}
const element = document.querySelector("html");
element.addEventListener("touchstart", (e) => {
// prevent swipe to navigate gesture
e.preventDefault();
});
var i=document.querySelector('#image');
var h = i.getBoundingClientRect().height;
var w = i.getBoundingClientRect().width;
origH=i.naturalHeight;
origW=i.naturalWidth;
$('#wrapper').css('width',w);
$('#wrapper').css('height',h);
/*
console.log('height: '+h);
console.log('width: '+w);
console.log('original-width:' + i.naturalWidth);
console.log('original-height:' + i.naturalHeight);
console.log('Scaling factor(h):' + scaling_factor_h);
console.log('Scaling factor(w):' + scaling_factor_w);
*/
scaling_factor_w= i.naturalWidth / w;
scaling_factor_h= i.naturalWidth / h;
var svg = $("svg").get(0);
svg.setAttribute('viewBox', '0 0 '+w+' '+h);
svg.setAttribute('width', w);
svg.setAttribute('height', h);
$('#p1').css({ top: $('#image').position().top, left: $('#image').position().left});
$('#p2').css({ top:0, left:w-40 });
$('#p3').css({ top:h-40, left:w-40 });
$('#p4').css({ top:h-40, left:0 });
var p1x = $('#p1').position().left + ($('#p1').width()/2);
var p1y = $('#p1').position().top + ($('#p1').height()/2);
var p2x=$('#p2').position().left + ($('#p2').width()/2);
var p2y=$('#p2').position().top + ($('#p2').height()/2);
var p3x=$('#p3').position().left + ($('#p3').width()/2);
var p3y=$('#p3').position().top + ($('#p3').height()/2);
var p4x=$('#p4').position().left + ($('#p4').width()/2);
var p4y=$('#p4').position().top + ($('#p4').height()/2);
var pointArray=[p1x,p1y,p2x,p2y,p3x,p3y,p4x,p4y];
$("#poly").attr("points", pointArray.join());
$("form").submit(function (event) {
var formData = {
originalSize:[origW, origH],
cropPoints: pointArray,
scaleFactors:[scaling_factor_w,scaling_factor_h],
};
$.ajax({
type: "POST",
url: "test.php",
data: formData,
dataType: "json",
encode: true,
}).done(function (data) {
console.log(data);
});
event.preventDefault();
});
});
</script>
<script type="module">
import interact from
'https://cdn.interactjs.io/v1.10.11/interactjs/index.js'
interact('.draggable').draggable({
modifiers: [
interact.modifiers.restrictRect({
restriction: 'parent',
endOnly: true
})
],
onmove: dragMoveListener,
listeners: {
move (event) {
/* console.log(event.pageX,
event.pageY) */
}
}
})
function dragMoveListener (event) {
var p1x = $('#p1').position().left + ($('#p1').width()/2);
var p1y = $('#p1').position().top + ($('#p1').height()/2);
var p2x=$('#p2').position().left + ($('#p2').width()/2);
var p2y=$('#p2').position().top + ($('#p2').height()/2);
var p3x=$('#p3').position().left + ($('#p3').width()/2);
var p3y=$('#p3').position().top + ($('#p3').height()/2);
var p4x=$('#p4').position().left + ($('#p4').width()/2);
var p4y=$('#p4').position().top + ($('#p4').height()/2);
var pointArray=[p1x,p1y,p2x,p2y,p3x,p3y,p4x,p4y];
$("#poly").attr("points", pointArray.join());
var target = event.target,
// keep the dragged position in the data-x/data-y attributes
x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx,
y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;
// translate the element
target.style.webkitTransform = target.style.transform
= 'translate(' + x + 'px, ' + y + 'px)';
// update the posiion attributes
target.setAttribute('data-x', x);
target.setAttribute('data-y', y);
}
</script>
</body>
</html>