-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhone-case.html
115 lines (92 loc) · 3.63 KB
/
Phone-case.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
<!DOCTYPE html>
<html>
<head>
<title>Phone Outline Generator</title>
<style>
.phone-frame {
position: relative;
width: 300px;
height: 600px;
background-color: #000;
margin: 0 auto;
margin-top: 50px;
border-radius: 10px;
overflow: hidden;
}
.camera {
position: absolute;
top: 10px;
left: 50%;
transform: translateX(-50%);
width: 50px;
height: 50px;
background-color: #fff;
cursor: move;
}
</style>
</head>
<body>
<div class="phone-frame" id="phone-frame">
<div class="camera" id="camera"></div>
</div>
<form id="phone-form">
<label for="width-input">Phone Width:</label>
<input type="text" id="width-input" placeholder="Enter phone width in pixels">
<label for="height-input">Phone Height:</label>
<input type="text" id="height-input" placeholder="Enter phone height in pixels">
<label for="camera-size-input">Camera Size:</label>
<input type="text" id="camera-size-input" placeholder="Enter camera size in pixels">
<label for="camera-position-input">Camera Position:</label>
<input type="text" id="camera-position-input" placeholder="Enter camera position from top in pixels">
<button type="submit">Generate</button>
</form>
<script>
var phoneFrame = document.getElementById("phone-frame");
var camera = document.getElementById("camera");
phoneFrame.addEventListener("click", function(event) {
if (event.target === camera) {
return; // Prevent moving the camera when clicking on it
}
var rect = phoneFrame.getBoundingClientRect();
var offsetX = event.clientX - rect.left;
var offsetY = event.clientY - rect.top;
var cameraSize = parseInt(document.getElementById("camera-size-input").value);
camera.style.left = offsetX - cameraSize / 2 + "px";
camera.style.top = offsetY - cameraSize / 2 + "px";
});
camera.addEventListener("mousedown", function(event) {
event.preventDefault(); // Prevent selecting text while dragging
var shiftX = event.clientX - camera.getBoundingClientRect().left;
var shiftY = event.clientY - camera.getBoundingClientRect().top;
moveCamera(event.clientX, event.clientY);
function moveCamera(clientX, clientY) {
var newLeft = clientX - shiftX - phoneFrame.getBoundingClientRect().left;
var newTop = clientY - shiftY - phoneFrame.getBoundingClientRect().top;
var cameraSize = parseInt(document.getElementById("camera-size-input").value);
var maxLeft = phoneFrame.offsetWidth - cameraSize;
var maxTop = phoneFrame.offsetHeight - cameraSize;
newLeft = Math.min(Math.max(0, newLeft), maxLeft);
newTop = Math.min(Math.max(0, newTop), maxTop);
camera.style.left = newLeft + "px";
camera.style.top = newTop + "px";
}
function onMouseMove(event) {
moveCamera(event.clientX, event.clientY);
}
function onMouseUp() {
document.removeEventListener("mousemove", onMouseMove);
document.removeEventListener("mouseup", onMouseUp);
}
document.addEventListener("mousemove", onMouseMove);
document.addEventListener("mouseup", onMouseUp);
});
document.getElementById("phone-form").addEventListener("submit", function(event) {
event.preventDefault();
var phoneWidth = document.getElementById("width-input").value;
var phoneHeight = document.getElementById("height-input").value;
phoneFrame.style.width = phoneWidth + "px";
phoneFrame.style.height = phoneHeight + "px";
});
</script>
</body>
</html>