Skip to content

Commit

Permalink
Update painter example
Browse files Browse the repository at this point in the history
  • Loading branch information
jnordberg committed May 1, 2017
1 parent f2a9a24 commit 4c8759f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
Binary file removed examples/painter/canvas.png
Binary file not shown.
11 changes: 6 additions & 5 deletions examples/painter/client/contents/paint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ if (window.performance) {
now = () => Date.now()
}

const client = new Client('ws://192.168.1.33:4242', Painter, {
const client = new Client('ws://localhost:4242', Painter, {
sendTimeout: 5000,
eventTypes: {
paint: PaintEvent,
Expand Down Expand Up @@ -156,14 +156,16 @@ window.addEventListener('DOMContentLoaded', async () => {
const dt = event.pos.timestamp - event.lastPos.timestamp
velocity = Math.sqrt(dx*dx + dy*dy) / dt
}
client.service.paint({
const msg = {
x: event.pos.x,
y: event.pos.y,
color: event.color,
size: 20 + velocity * 20,
}).catch((error: Error) => {
size: 20 + velocity * 20
}
client.service.paint(msg).catch((error: Error) => {
console.warn('error drawing', error.message)
})
shared.paint(msg, ctx)
}

let mouseDraw: DrawEvent|undefined
Expand Down Expand Up @@ -246,5 +248,4 @@ window.addEventListener('DOMContentLoaded', async () => {
})

console.log(' ;-) ')
window['colors'] = colors
window['client'] = client
10 changes: 8 additions & 2 deletions examples/painter/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ const server = new wsrpc.Server({
service: proto.lookupService('Painter')
})

server.implement('paint', async (event) => {
server.implement('paint', async (event, sender) => {
shared.paint(event, ctx)
server.broadcast('paint', PaintEvent.encode(event).finish())
const broadcast = PaintEvent.encode(event).finish()
for (const connection of server.connections) {
if (connection === sender) {
continue
}
connection.send('paint', broadcast)
}
})

server.implement('getCanvas', async (request: CanvasRequest) => {
Expand Down
4 changes: 2 additions & 2 deletions examples/painter/shared/paint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {PaintEvent} from './../protocol/service'
import {IPaintEvent} from './../protocol/service'
const Canvas = require('canvas')

const brushSize = 124
Expand Down Expand Up @@ -51,7 +51,7 @@ function getBrush(color: number) {
return brush
}

export function paint(p: PaintEvent, ctx: CanvasRenderingContext2D) {
export function paint(p: IPaintEvent, ctx: CanvasRenderingContext2D) {
ctx.globalAlpha = 0.4
// ctx.globalCompositeOperation = 'overlay'
ctx.fillStyle = '#' + p.color.toString(16)
Expand Down

0 comments on commit 4c8759f

Please sign in to comment.