javascript - websocket (socket.io) 上的协作白板( Canvas )圆圈

标签 javascript html node.js canvas socket.io

我正在尝试使用 html5 canvas、nodejs 和 websockets(socket.io) 做一些协作(在线)白板应用程序,我的过程进展顺利,但我想画圆圈。实际上我可以画线,(例如)我认为圆就像线一样,线的起点必须是圆的中心,线的起点和终点之间的距离必须是圆的半径。但我无法将这个想法转换为代码。 :(

我的绘制函数的客户端代码。

function draw(data, fromMe){
        if(DP.thisObj[data.id]){
            var eventType = _eventTypes(data.isTouchDevice),
            ctx = DP.thisObj[data.id].ctx,
            scratchCtx = DP.thisObj.scratch.ctx;

            //set the ctx
            ctx.strokeStyle = data.color;
            ctx.lineWidth = data.stroke;
            ctx.lineCap = "round";
            ctx.lineJoin = "round";

            scratchCtx.strokeStyle = data.color;
            scratchCtx.lineWidth = data.stroke;
            scratchCtx.lineCap = "round";
            scratchCtx.lineJoin = "round";

            if(data.isErase){
                ctx.globalCompositeOperation = "destination-out";
                scratchCtx.globalCompositeOperation = "destination-out";
            } else {
                ctx.globalCompositeOperation = "source-over";
                scratchCtx.globalCompositeOperation = "source-over";
            }


            if (data.type === eventType.down) {     
                DP.okToDraw = true;
                if(fromMe && !data.isLineDrawing){
                    DP.points.push({x : data.x, y : data.y});
                } else if(data.isLineDrawing) { //for line drawing we only need the coords
                    DP.thisObj[data.id].x = data.x;
                    DP.thisObj[data.id].y = data.y;
                } else { //from a shared canvas
                    ctx.beginPath();
                    ctx.moveTo(data.x, data.y);
                }
            } else if ((data.type === eventType.move) && DP.okToDraw) {


                if(data.isLineDrawing && fromMe) {  //draw the line on a temp canvas
                    scratchCtx.clearRect(0, 0, DP.myCanvas.width, DP.myCanvas.height);
                    scratchCtx.beginPath();
                    scratchCtx.moveTo(DP.thisObj[data.id].x, DP.thisObj[data.id].y);
                    scratchCtx.lineTo(data.x, data.y);
                    scratchCtx.stroke();
                } else if(fromMe){
                    scratchCtx.clearRect(0, 0, DP.myCanvas.width, DP.myCanvas.height); 
                    DP.points.push({x : data.x, y : data.y});
                    _drawPoints(scratchCtx);
                } else if(!data.isLineDrawing) { //this is coming from drawing a shared canvas
                    ctx.lineTo(data.x, data.y);
                    ctx.stroke();
                }
            } else if(data.type === eventType.up){
                if(data.isLineDrawing) {    //when done put the scratch line on the scratch canvas
                    ctx.beginPath();
                    ctx.moveTo(DP.thisObj[data.id].x, DP.thisObj[data.id].y);
                    ctx.lineTo(data.x, data.y);
                    ctx.stroke();
                    ctx.closePath();
                    scratchCtx.clearRect(0, 0, DP.myCanvas.width, DP.myCanvas.height);
                } else if(fromMe){  
                    ctx.drawImage(DP.scratchCanvas, 0, 0);
                    scratchCtx.clearRect(0, 0, DP.myCanvas.width, DP.myCanvas.height);
                } else {
                    ctx.closePath();
                }
                DP.okToDraw = false;
                scratchCtx.closePath();

                DP.points = [];
            }
        }

    }

这是用于绘制函数的服务器端代码。服务器.js

(function () {
    var connectedClients = {}, //used to keep a working list of the connections
        io = require('socket.io').listen(4000);

    io.sockets.on('connection', function (socket) {


        //drawing data
        socket.on('drawRequest', function (data) {
            socket.broadcast.emit('draw', {
                x: data.x,
                y: data.y,
                type: data.type,
                isTouchDevice : data.isTouchDevice,
                color: data.color,
                stroke: data.stroke,
                isLineDrawing: data.isLineDrawing,
                isErase: data.isErase,
                id: data.id
            });
        });

    });
}).call(this);

最佳答案

你需要一个

arc(x, y, radius, startAngle, endAngle, anticlockwise)

DMO

var radius = 70;
context.beginPath();
context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
context.fillStyle = 'green';
context.fill();
context.lineWidth = 5;
context.strokeStyle = '#003300';
context.stroke();

关于javascript - websocket (socket.io) 上的协作白板( Canvas )圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23509398/

相关文章:

javascript - Bluemix 单点登录不显示登录页面

php - 2 个文本字段输入到 1 条记录中

node.js - 将 Cloud9 IDE 部署到 Heroku?

node.js - gulp 构建输出不在 Heroku slug 中

JavaScript <form> 输入字段 "associative"数组?

javascript - AngularJS 下拉菜单在提交前需要验证

javascript - 将curl POST 请求作为npm 脚本执行

html - 一些链接在 css 样式使字母变为粗体后不会显示为粗体

javascript - 将滚动条的宽度添加到窗口宽度

angularjs - 如何在虚拟机上访问我的工作 Angular 2 应用程序