forked from vishanurag/Canvas-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basictemp.html
157 lines (140 loc) · 4.46 KB
/
basictemp.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Template Editor</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial, sans-serif;
}
.editor-container {
display: flex;
height: 100vh;
}
.sidebar {
width: 200px;
background-color: #4caf50;
color: #fff;
padding: 20px;
}
.sidebar h2 {
margin-bottom: 20px;
}
.sidebar button {
display: block;
width: 100%;
padding: 10px;
margin: 10px 0;
background-color: #fff;
color: #4caf50;
border: none;
cursor: pointer;
}
.sidebar button:hover {
background-color: #e7e7e7;
}
.canvas {
flex: 1;
background-color: #f0f0f0;
position: relative;
overflow: hidden;
}
.canvas-element {
position: absolute;
border: 1px solid #ccc;
cursor: move;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
</style>
</head>
<body>
<div class="editor-container">
<!-- Sidebar for tools -->
<div class="sidebar">
<h2>Tools</h2>
<button onclick="addText()">Add Text</button>
<button onclick="addImage()">Add Image</button>
<button onclick="addShape()">Add Shape</button>
</div>
<!-- Canvas area -->
<div class="canvas" id="canvas">
<!-- Elements will be added dynamically here -->
</div>
</div>
<script>
// To add text box
function addText() {
const text = document.createElement('div');
text.classList.add('canvas-element');
text.contentEditable = true;
text.style.left = '50px';
text.style.top = '50px';
text.innerText = 'Edit text here';
document.getElementById('canvas').appendChild(text);
makeDraggable(text);
}
// To add image placeholder
function addImage() {
const image = document.createElement('div');
image.classList.add('canvas-element');
image.style.left = '100px';
image.style.top = '100px';
image.style.width = '100px';
image.style.height = '100px';
image.style.backgroundColor = '#ddd';
image.innerText = 'Image';
document.getElementById('canvas').appendChild(image);
makeDraggable(image);
}
// To add shape
function addShape() {
const shape = document.createElement('div');
shape.classList.add('canvas-element');
shape.style.left = '150px';
shape.style.top = '150px';
shape.style.width = '100px';
shape.style.height = '100px';
shape.style.backgroundColor = '#4caf50';
shape.innerText = 'Shape';
document.getElementById('canvas').appendChild(shape);
makeDraggable(shape);
}
// Make elements draggable
function makeDraggable(element) {
let posX = 0, posY = 0, initialX = 0, initialY = 0;
element.onmousedown = dragMouseDown;
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
initialX = e.clientX;
initialY = e.clientY;
document.onmouseup = closeDragElement;
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
posX = initialX - e.clientX;
posY = initialY - e.clientY;
initialX = e.clientX;
initialY = e.clientY;
element.style.top = (element.offsetTop - posY) + "px";
element.style.left = (element.offsetLeft - posX) + "px";
}
function closeDragElement() {
document.onmouseup = null;
document.onmousemove = null;
}
}
</script>
</body>
</html>