Skip to content

Commit ba305e4

Browse files
committed
Refactor SVG interaction and styles: updated CSS for cell transitions and added toggle functionality for SVG enlargement in JavaScript.
1 parent 459afad commit ba305e4

File tree

2 files changed

+69
-29
lines changed

2 files changed

+69
-29
lines changed

css/secrets.css

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,16 @@ body {
66
}
77

88
svg {
9-
margin: 8px;
9+
margin: 0;
1010
}
1111

1212
.cell {
1313
padding: 0;
1414
margin: 0;
15-
16-
-webkit-transform: rotate(0deg);
1715
transform: rotate(0deg);
18-
-webkit-transition: -webkit-transform 5s;
19-
transition: transform 5s;
20-
16+
transition: transform 5s, width 1s, height 1s;
2117
}
18+
2219
.cell:hover {
2320
transform: rotate(90deg);
24-
-webkit-transform: rotate(90deg);
25-
26-
}
21+
}

js/secrets.js

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ function addSvgs(size, rows, cols) {
5959
shape.translation.set(two.width / 2, two.height / 2);
6060
two.add(shape);
6161
two.update();
62+
63+
$(o).on("click", function() {
64+
toggleSVG($(o), two, shape);
65+
});
6266
});
6367
}
6468

@@ -113,23 +117,64 @@ function pickFlower(flowers) {
113117
flower.cap = 'round';
114118
return flower;
115119
}
116-
//
117-
// function getHeight(element)
118-
// {
119-
// element.style.visibility = "hidden";
120-
// document.body.appendChild(element);
121-
// var height = element.offsetHeight + 0;
122-
// document.body.removeChild(element);
123-
// element.style.visibility = "visible";
124-
// return height;
125-
// }
126-
//
127-
// function getWidth(element)
128-
// {
129-
// element.style.visibility = "hidden";
130-
// document.body.appendChild(element);
131-
// var width = element.offsetWidth + 0;
132-
// document.body.removeChild(element);
133-
// element.style.visibility = "visible";
134-
// return width;
135-
// }
120+
121+
122+
function toggleSVG(cell, two, shape) {
123+
var isEnlarged = cell.data("isEnlarged");
124+
125+
if (isEnlarged) {
126+
// Shrink back to original size
127+
cell.css({
128+
position: "absolute",
129+
width: two.width,
130+
height: two.height,
131+
zIndex: 1
132+
});
133+
134+
shape.translation.set(two.width / 2, two.height / 2);
135+
shape.scale = 1;
136+
// Remove text if it exists
137+
var text = shape.children.find(child => child instanceof Two.Text);
138+
if (text) {
139+
shape.remove(text);
140+
}
141+
two.update();
142+
} else {
143+
// Enlarge to cover the entire screen
144+
var newWidth = $(window).width();
145+
var newHeight = $(window).height();
146+
var newCenterX = newWidth / 2;
147+
var newCenterY = newHeight / 2;
148+
var scaleFactor = Math.max(newWidth / two.width, newHeight / two.height);
149+
150+
cell.css({
151+
position: "fixed",
152+
top: 0,
153+
left: 0,
154+
width: "100vw",
155+
height: "100vh",
156+
zIndex: 9999,
157+
transition: "width 1s, height 1s"
158+
});
159+
160+
two.width = newWidth;
161+
two.height = newHeight;
162+
two.update();
163+
164+
165+
166+
two.bind("update", function() {
167+
shape.scale += (scaleFactor - shape.scale) * 0.1;
168+
shape.translation.set(newCenterX, newCenterY);
169+
170+
if (Math.abs(shape.scale - scaleFactor) < 0.01) {
171+
shape.scale = scaleFactor;
172+
two.unbind("update");
173+
}
174+
});
175+
176+
two.play();
177+
}
178+
179+
cell.data("isEnlarged", !isEnlarged);
180+
}

0 commit comments

Comments
 (0)