work on drawing demo

This commit is contained in:
Kevin Jahns 2017-12-06 19:20:52 -08:00
parent 4efa16e2dd
commit 8105bef1af
2 changed files with 73 additions and 84 deletions

View File

@ -12,12 +12,8 @@
</style>
<button type="button" id="clearDrawingCanvas">Clear Drawing</button>
<svg id="drawingCanvas" viewbox="0 0 100 100" width="100%"></svg>
<script src="../../y.js"></script>
<script src="../../../y-array/y-array.js"></script>
<script src="../../../y-map/dist/y-map.js"></script>
<script src="../../../y-memory/y-memory.js"></script>
<script src="../../../y-websockets-client/y-websockets-client.js"></script>
<script src="../bower_components/d3/d3.js"></script>
<script src="../yjs-dist.js"></script>
<script src="./canvasjs.min.js"></script>
<script src="./index.js"></script>
</body>
</html>

View File

@ -1,20 +1,14 @@
/* globals Y, d3 */
'strict mode'
Y({
db: {
name: 'memory'
},
let y = new Y({
connector: {
name: 'websockets-client',
room: 'drawing-example',
url: 'localhost:1234'
},
sourceDir: '/bower_components',
share: {
drawing: 'Array'
url: 'http://127.0.0.1:1234',
room: 'drawing-example'
// maxBufferLength: 100
}
}).then(function (y) {
})
window.yDrawing = y
var drawing = y.share.drawing
var renderPath = d3.svg.line()
@ -33,22 +27,22 @@ Y({
var line = svg.append('path').datum(yarray.toArray())
line.attr('d', renderPath)
yarray.observe(function (event) {
// we only implement insert events that are appended to the end of the array
event.values.forEach(function (value) {
line.datum().push(value)
})
line.remove()
line = svg.append('path').datum(yarray.toArray())
line.attr('d', renderPath)
})
}
// call drawLine every time an array is appended
y.share.drawing.observe(function (event) {
if (event.type === 'insert') {
event.values.forEach(drawLine)
} else {
// just remove all elements (thats what we do anyway)
event.changedKeys.forEach(function (key) {
let path = y.share.drawing.get(key)
if (path === undefined) {
svg.selectAll('path').remove()
} else {
drawLine(path)
}
})
})
// draw all existing content
for (var i = 0; i < drawing.length; i++) {
drawLine(drawing.get(i))
@ -81,4 +75,3 @@ Y({
window.clearTimeout(ignoreDrag)
ignoreDrag = null
}
})