-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
87 lines (77 loc) · 2.01 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
let rotator = 0
let plant, canvasElement
let modalShown = false
const modalWrapper = document.getElementById('modal')
const modalContent = document.getElementById('modal-content')
const inputField = document.getElementById('code-input')
const inputLabel = document.getElementById('code-label')
function loadRandomPlant () {
noLoop()
plant = new Plant()
loop()
}
function loadPlantFromCode () {
let inputValidated
if (!inputField.value) {
showModalMessage('Enter plant code', 2)
}
else {
inputValidated = parser.parseInput(inputField.value)
if (!inputValidated)
{
showModalMessage('Invalid code', 2)
}
else {
noLoop()
plant = new Plant(inputValidated)
loop()
showModalMessage('Plant loaded', 2)
}
inputField.value = ''
}
}
function copyPlantToClipboard () {
const code = parser.plantToCode(plant.log())
const successMessage = 'Plant code copied to clipboard'
const failureMessage = 'Cannot access clipboard, use the console (F12)'
navigator.clipboard.writeText(code)
.then(showModalMessage(successMessage, 2))
.catch(() => {
showModalMessage(failureMessage, 3)
console.log('Your plant code:')
console.log(code)
})
}
function showModalMessage (message, time) {
if (!modalShown) {
modalShown = true
modalWrapper.classList.remove('modal-disabled')
modalContent.textContent = message
setTimeout(() => {
modalWrapper.classList.add('modal-disabled')
modalShown = false
}, time * 1000)
}
}
function riseLabel () {
inputLabel.classList.add('risen')
}
function lowerLabel () {
inputLabel.classList.remove('risen')
}
inputField.addEventListener('focusin', riseLabel)
inputField.addEventListener('focusout', lowerLabel)
function setup() {
canvasElement = createCanvas(400, 400, WEBGL)
canvasElement.parent('canvas-wrapper')
frameRate(30)
plant = new Plant()
}
function draw() {
background(220)
translate(0, 150, -150)
rotateX(PI - 0.4)
rotateY(rotator)
rotator += 0.01
plant.draw()
}