You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
7.1 KiB

document.addEventListener('DOMContentLoaded', main);
2 years ago
function draw(state, context) {
state.timers.raf = false;
2 years ago
const gl = context.gl;
const width = window.innerWidth;
const height = window.innerHeight;
2 years ago
let locations;
let buffers;
2 years ago
gl.viewport(0, 0, context.canvas.width, context.canvas.height);
gl.clearColor(context.bgcolor.r, context.bgcolor.g, context.bgcolor.b, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
2 years ago
// Draw images
locations = context.locations['quad'];
buffers = context.buffers['quad'];
gl.useProgram(context.programs['quad']);
2 years ago
gl.enableVertexAttribArray(locations['a_pos']);
2 years ago
gl.enableVertexAttribArray(locations['a_texcoord']);
2 years ago
gl.uniform2f(locations['u_res'], context.canvas.width, context.canvas.height);
gl.uniform2f(locations['u_scale'], state.canvas.zoom, state.canvas.zoom);
gl.uniform2f(locations['u_translation'], state.canvas.offset.x, state.canvas.offset.y);
gl.uniform1i(locations['u_layer'], 0);
2 years ago
2 years ago
gl.bindBuffer(gl.ARRAY_BUFFER, buffers['b_pos']);
gl.vertexAttribPointer(locations['a_pos'], 2, gl.FLOAT, false, 0, 0);
gl.bufferData(gl.ARRAY_BUFFER, context.quad_positions_f32, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, buffers['b_texcoord']);
gl.vertexAttribPointer(locations['a_texcoord'], 2, gl.FLOAT, false, 0, 0);
gl.bufferData(gl.ARRAY_BUFFER, context.quad_texcoords_f32, gl.STATIC_DRAW);
2 years ago
let tex_index = 0;
for (const key in context.textures) {
gl.bindTexture(gl.TEXTURE_2D, context.textures[key]);
gl.drawArrays(gl.TRIANGLES, tex_index * 6, 6);
++tex_index;
}
2 years ago
// Draw strokes
locations = context.locations['stroke'];
buffers = context.buffers['stroke'];
gl.useProgram(context.programs['stroke']);
gl.enableVertexAttribArray(locations['a_pos']);
gl.enableVertexAttribArray(locations['a_color']);
gl.uniform2f(locations['u_res'], context.canvas.width, context.canvas.height);
gl.uniform2f(locations['u_scale'], state.canvas.zoom, state.canvas.zoom);
gl.uniform2f(locations['u_translation'], state.canvas.offset.x, state.canvas.offset.y);
gl.uniform1i(locations['u_layer'], 1);
2 years ago
const total_pos_size = context.static_positions_f32.byteLength + context.dynamic_positions_f32.byteLength;
const total_color_size = context.static_colors_u8.byteLength + context.dynamic_colors_u8.byteLength;
2 years ago
const total_point_count = (context.static_positions.length + total_dynamic_positions(context)) / 2;
2 years ago
gl.bindBuffer(gl.ARRAY_BUFFER, buffers['b_pos']);
gl.vertexAttribPointer(locations['a_pos'], 2, gl.FLOAT, false, 0, 0);
gl.bufferData(gl.ARRAY_BUFFER, total_pos_size, gl.DYNAMIC_DRAW);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, context.static_positions_f32);
gl.bufferSubData(gl.ARRAY_BUFFER, context.static_positions_f32.byteLength, context.dynamic_positions_f32);
gl.bindBuffer(gl.ARRAY_BUFFER, buffers['b_color']);
gl.vertexAttribPointer(locations['a_color'], 3, gl.UNSIGNED_BYTE, true, 0, 0);
gl.bufferData(gl.ARRAY_BUFFER, total_color_size, gl.DYNAMIC_DRAW);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, context.static_colors_u8);
gl.bufferSubData(gl.ARRAY_BUFFER, context.static_colors_u8.byteLength, context.dynamic_colors_u8);
gl.drawArrays(gl.TRIANGLES, 0, total_point_count);
}
2 years ago
const config = {
ws_url: 'ws://192.168.100.2/ws/',
image_url: 'http://192.168.100.2/images/',
sync_timeout: 1000,
2 years ago
ws_reconnect_timeout: 10000,
2 years ago
brush_preview_timeout: 1000,
2 years ago
second_finger_timeout: 500,
buffer_first_touchmoves: 5,
2 years ago
debug_print: true,
2 years ago
min_zoom: 0.01,
max_zoom: 100.0,
2 years ago
};
2 years ago
const EVENT = Object.freeze({
PREDRAW: 10,
STROKE: 20,
RULER: 21, /* gets re-written with EVENT.STROKE before sending to server */
UNDO: 30,
REDO: 31,
IMAGE: 40,
IMAGE_MOVE: 41,
ERASER: 50,
});
const MESSAGE = Object.freeze({
INIT: 100,
SYN: 101,
ACK: 102,
FULL: 103,
FIRE: 104,
JOIN: 105,
});
function event_size(event) {
let size = 1 + 3; // type + padding
switch (event.type) {
case EVENT.PREDRAW: {
size += 4 * 2;
break;
}
case EVENT.STROKE: {
size += 4 + 2 + 2 + 4 + event.points.length * 4 * 2; // u32 stroke id + u16 (count) + u16 (width) + u32 (color + count * (f32, f32) points
break;
}
case EVENT.UNDO:
case EVENT.REDO: {
break;
}
case EVENT.IMAGE:
case EVENT.IMAGE_MOVE: {
size += 4 + 4 + 4; // file id + x + y
break;
}
case EVENT.ERASER: {
size += 4; // stroke id
break;
}
default: {
console.error('fuck');
}
}
return size;
}
function main() {
2 years ago
const state = {
2 years ago
'me': 333,
2 years ago
'canvas': {
'offset': { 'x': 0, 'y': 0 },
'zoom': 1.0,
},
2 years ago
'cursor': {
'x': 0,
'y': 0,
},
2 years ago
'sn': 0,
'lsn': 0,
'server_lsn': 0,
'stroke_width': 8,
2 years ago
'touch': {
'moves': 0,
'drawing': false,
'moving': false,
'waiting_for_second_finger': false,
'first_finger_position': null,
'second_finger_position': null,
'buffered': [],
'ids': [],
},
2 years ago
'moving': false,
'drawing': false,
'spacedown': false,
2 years ago
'current_strokes': {},
'fire_queue': [],
2 years ago
'queue': [],
'events': [],
2 years ago
'tools': {
'active': null,
'active_element': null,
},
2 years ago
'colors': {
'active': null,
'active_element': null,
},
2 years ago
'timers': {
'ws_reconnect': null,
2 years ago
'hide_preview': null,
'raf': false,
2 years ago
},
'players': {},
2 years ago
};
2 years ago
const context = {
'canvas': null,
'gl': null,
2 years ago
'programs': {},
2 years ago
'buffers': {},
'locations': {},
2 years ago
'textures': {},
2 years ago
'dynamic_positions': {},
'dynamic_colors': {},
2 years ago
'quad_positions': [],
'quad_texcoords': [],
2 years ago
'static_positions': [],
2 years ago
'static_colors': [],
'static_positions_f32': new Float32Array(0),
'dynamic_positions_f32': new Float32Array(0),
'static_colors_u8': new Uint8Array(0),
'dynamic_colors_u8': new Uint8Array(0),
2 years ago
'quad_positions_f32': new Float32Array(0),
'quad_texcoords_f32': new Float32Array(0),
2 years ago
'bgcolor': {'r': 1.0, 'g': 1.0, 'b': 1.0},
2 years ago
};
2 years ago
const url = new URL(window.location.href);
const parts = url.pathname.split('/');
state.desk_id = parts.length > 0 ? parts[parts.length - 1] : 0;
2 years ago
init_webgl(state, context);
init_listeners(state, context);
init_tools(state);
2 years ago
ws_connect(state, context, true);
schedule_draw(state, context);
}
function schedule_draw(state, context) {
if (!state.timers.raf) {
window.requestAnimationFrame(() => draw(state, context));
state.timers.raf = true;
}
}