generated from drawwithcode/P5-empty-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
118 lines (98 loc) · 3.39 KB
/
sketch.js
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
let privacyStatementP5;
let agreeAndSave;
let agreeAndSaveEmoji;
let learnMore;
let learnMoreEmoji;
let privacyStatementJS;
let privacyStatementBoundingClientRect;
let privacyStatementBoundingClientRectBottom;
let difference;
let posY;
function setup() {
// create the canvas inside #container"
createCanvas(windowWidth, windowHeight).parent("container");
// styling
noFill();
rectMode(CENTER);
strokeWeight(1);
stroke(255);
getDivAndCanvas();
}
function draw() {
clear();
line(0, 0, width, height);
line(width, 0, 0, height);
/* - draws a rectangle inside the canvas
- sadly its top border isn't viewable on some browsers (Chrome on Android)
- temporarily substituted by a div (.boundingBox), which has a similar problem
*/
// push();
// strokeWeight(2);
// rect(width / 2, height / 2, windowWidth, windowHeight);
// pop();
privacyStatementP5 = select(".privacyStatement");
agreeAndSave = select("#agreeAndSave");
agreeAndSaveEmoji = select("#agreeAndSaveEmoji");
learnMore = select("#learnMore");
learnMoreEmoji = select("#learnMoreEmoji");
// div animation
if (width > height) {
// horizontal viewport
posY = difference * Math.pow(sin((frameCount / width) * 4), 2);
} else {
// vertical viewport
posY = difference * Math.pow(sin(frameCount / height / 2), 2);
}
privacyStatementP5.position(0, -posY);
// desktop callback functions
agreeAndSave.mouseOver(agreeAndSaveEmojiShow);
agreeAndSave.mouseOut(agreeAndSaveEmojiHide);
learnMore.mouseOver(learnMoreEmojiShow);
learnMore.mouseOut(learnMoreEmojiHide);
// smartphone callback functions
agreeAndSave.touchStarted(agreeAndSaveEmojiShow);
agreeAndSave.touchMoved(agreeAndSaveEmojiShow);
agreeAndSave.touchEnded(agreeAndSaveEmojiHide);
learnMore.touchStarted(learnMoreEmojiShow);
learnMore.touchMoved(learnMoreEmojiShow);
learnMore.touchEnded(learnMoreEmojiHide);
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
// reset the position of #privacyStatement
privacyStatementP5.position(0, 0);
getDivAndCanvas();
}
function learnMoreToggle() {
if (privacyStatementP5.class() == "privacyStatement hide") {
privacyStatementP5.class("privacyStatement show");
} else if (privacyStatementP5.class() == "privacyStatement show") {
privacyStatementP5.class("privacyStatement hide");
}
}
function agreeAndSaveEmojiShow() {
agreeAndSaveEmoji.class("emoji show");
}
function agreeAndSaveEmojiHide() {
agreeAndSaveEmoji.class("emoji hide");
}
function learnMoreEmojiShow() {
learnMoreEmoji.class("emoji show");
}
function learnMoreEmojiHide() {
learnMoreEmoji.class("emoji hide");
}
// this function calculates the dimensions of #privacyStatement", which can then be used to move it with its contents
// it restates the variables so that the div moves correctly even when the viewport changes dimensions.
// for further info, see https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
function getDivAndCanvas() {
privacyStatementJS = document.getElementById("privacyStatement");
privacyStatementBoundingClientRect =
privacyStatementJS.getBoundingClientRect();
privacyStatementBoundingClientRectBottom =
privacyStatementBoundingClientRect.bottom;
difference = privacyStatementBoundingClientRectBottom - height;
}
function savingPage() {
window.open("/2021-04-benedettoandrea/savingPage/savingPage.html", "_self");
}