forked from HackWalls/HackWalls
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
150 lines (138 loc) · 4.58 KB
/
index.js
File metadata and controls
150 lines (138 loc) · 4.58 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
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
(function() {
var $ = function(id){return document.getElementById(id)}
// GLOBALS!
// change this to update the strokes
window.paintConfig = {
color: 'blue',
strokeWidth: 15
}
window.hammer = new Hammer(document.querySelector('#main'))
// not globals :(
var gWidth = 1000;//window.innerWidth > 0 ? window.innerWidth : screen.width
var gHeight = 1000;//window.innerHeight > 0 ? window.innerHeight : screen.height
var canvas = this.__canvas = new fabric.Canvas('c', {
// isDrawingMode: true,
width: gWidth,
height: gHeight
})
canvas.selection = false
Y({
db: {
name: 'memory'
},
connector: {
name: 'webrtc',
room: 'hackAR5',
url: 'https://yjs.dbis.rwth-aachen.de:5078'//'http://134.61.141.107:1234'//'https://yjs.dbis.rwth-aachen.de:5078'
},
sourceDir: './yjs',
share: {
drawings: 'Array' // y.share.textarea is of type Y.Text
}
}).then(function (y) {
window.y = y
function computePath(drawingPath, yarray) {
// array should contain additional information (as object) as the first elemnt
// the rest are path coordinates
var array = yarray.toArray().map(function (c, i) {
if (i !== 0) {
return [c[0]*gWidth, c[1]*gHeight] // relative coordinates to absolute coordinates
} else {
return c
}
})
var drawingPathProperties = array[0]
// don't return anything if incomplete
if (array.length > 2) {
// transform path to bezier courve
var path = [['M', array[1][0], array[1][1]]]
for (var i = 2; i < array.length - 1; i++) {
path.push(['Q', array[i-1][0], array[i-1][1], array[i][0], array[i][1]])
}
path.push(['L', array[array.length - 1][0], array[array.length - 1][1]])
drawingPath.path = path
} else {
drawingPath.path = []
drawingPathProperties = {
color: null,
strokeWidth: 7
}
}
drawingPath.stroke = drawingPathProperties.color
drawingPath.strokeWidth = drawingPathProperties.strokeWidth
// render path
var dims = drawingPath._parseDimensions()
drawingPath.setWidth(dims.width)
drawingPath.setHeight(dims.height)
drawingPath.pathOffset.x = drawingPath.width/2
drawingPath.pathOffset.y = drawingPath.height/2
drawingPath.setCoords()
canvas.renderAll()
}
function drawElement (t) {
if (t instanceof Y.Array.typeDefinition.class) {
var path = new fabric.Path()
path.selectable = false
path.fill = null
canvas.add(path)
computePath(path, t)
t.observe(function (events) {
events.forEach(function (e) {
computePath(path, t)
})
})
}
}
for (var i = 0; i < y.share.drawings.length; i++) {
y.share.drawings.get(i).then(drawElement)
}
y.share.drawings.observe(function (events) {
events.forEach(function (e) {
e.values().then(function (types) {
types.forEach(drawElement)
})
})
})
})
var yPath = false
// listen to events...
hammer.on('pan', function (event) {
var xPos = event.center.x
var yPos = event.center.y
var main = document.querySelector('#main');
xPos-=main.offsetLeft;
yPos-=main.offsetTop;
//xPos/= document.getElementById("c").getAttribute("width") / document.getElementById("main").offsetWidth;
//yPos /= document.getElementById("c").getAttribute("height")/ document.getElementById("main").offsetHeight;
var w = document.getElementById("c").offsetWidth;
var h = document.getElementById("c").offsetHeight;
//console.log(w);
if(window.superTransfromMatrix){
var newPos = window.superTransfromMatrix.transformInverse(xPos, yPos);
xPos=newPos[0];
yPos=newPos[1];
}
xPos *= 1000/w
yPos *= 1000/h
if (yPath === false) {
yPath = null // trying to set yPath..
var pos = y.share.drawings.length
y.share.drawings.insert(pos, [Y.Array])
y.share.drawings.get(pos).then(function (array) {
yPath = array
var strokeWidth = paintConfig.strokeWidth
yPath.insert(0, [{
color: paintConfig.color,
strokeWidth: strokeWidth
}, [(xPos - strokeWidth) / gWidth, (yPos - strokeWidth) / gHeight]])
})
} else if (yPath != null) {
var strokeWidth = yPath.get(0).strokeWidth
yPath.push([[(xPos - strokeWidth) / gWidth, (yPos - strokeWidth) / gHeight]])
}
if (event.isFinal) {
yPath = false
}
})
fabric.Object.prototype.transparentCorners = false
})()