-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
97 lines (88 loc) · 2.82 KB
/
Copy pathscript.js
File metadata and controls
97 lines (88 loc) · 2.82 KB
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
const rec = (x, y, i, obj) => {
if (i === 0) {
return
}
const randomX = Math.round(Math.random() * 100);
const randomX2 = Math.round(Math.random() * 100);
const randomY = Math.round(Math.random() * 100);
const randomY2 = Math.round(Math.random() * 100);
const randomNum = Math.random()
if (randomX > 70) {
obj.beginPath()
obj.lineWidth = i;
obj.moveTo(x, y)
obj.lineTo(randomNum >= 0.5 ? x + randomX : x - randomX, y- randomY)
obj.strokeStyle = 'blue'
obj.stroke()
rec(randomNum >= 0.5 ? x + randomX : x - randomX, y- randomY, i-1, ctx)
} else if (randomX < 20) {
obj.beginPath()
obj.lineWidth = i;
obj.moveTo(x, y)
obj.lineTo(randomNum >= 0.5 ? x + randomX : x - randomX, y- randomY)
obj.strokeStyle = 'blue'
obj.stroke()
} else {
obj.beginPath()
obj.lineWidth = i;
obj.moveTo(x, y)
obj.lineTo(x +randomX, y- randomY)
obj.moveTo(x, y)
obj.lineTo(x-randomX2, y - randomY2)
obj.strokeStyle = 'blue'
obj.stroke()
rec(x +randomX, y- randomY, i-1, ctx)
rec(x - randomX2, y-randomY2, i-1, ctx)
}
}
const canvasObject = document.getElementById('TestCanvas')
const okButton = document.getElementById('okButton')
const input = document.getElementById('inputValue')
canvasObject.width = document.documentElement.scrollWidth
canvasObject.height = document.documentElement.clientHeight - 200
const ctx = canvasObject.getContext('2d')
let mouseStatus = false
const coordinate = {
x: null,
y: null
}
canvasObject.addEventListener('mousedown', (e) => {
console.log(e.pageX, e.pageY)
coordinate.x = e.pageX
coordinate.y = e.pageY
mouseStatus = true
})
canvasObject.addEventListener('mouseup', (e) => {
mouseStatus = false
})
canvasObject.addEventListener('mouseout', (e) => {
mouseStatus = false
})
const startCoordinateTree = {
x: canvasObject.width/2,
y: canvasObject.height
}
okButton.addEventListener('click', (e)=>{
ctx.clearRect(0, 0, canvasObject.width, canvasObject.height);
ctx.beginPath()
ctx.lineWidth = +input.value + 1;
ctx.moveTo(startCoordinateTree.x, startCoordinateTree.y)
ctx.lineTo(startCoordinateTree.x, startCoordinateTree.y - 20)
ctx.strokeStyle = 'blue'
ctx.stroke()
rec(startCoordinateTree.x, startCoordinateTree.y - 20, +input.value, ctx)
})
canvasObject.addEventListener('mousemove', (e) => {
if (mouseStatus) {
if (coordinate.x === null || coordinate.y === null ) {
ctx.moveTo(e.pageX, e.pageY)
} else {
ctx.moveTo(coordinate.x, coordinate.y)
}
ctx.lineTo(e.x, e.y)
ctx.strokeStyle = 'grey'
ctx.stroke()
coordinate.x = e.pageX
coordinate.y = e.pageY
}
}, false)