你画我猜。canvas

 

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title>你画我猜</title>
    <style>
        html,
        body,
        div,
        canvas,
        nav,
        input,
        ul,
        li {
            margin: 0;
            padding: 0;
        }

        #box {
            width: 600px;
            height: 700px;
            margin: 0px auto;
            background-color: palevioletred;
        }

        #box nav {
            width: 100%;
            height: 150px;
        }

        #box nav div {
            height: 50px;
            line-height: 50px;
        }

        #box nav .changeColor {
            padding-left: 15px;
        }

        .changeColor input {
            width: 30px;
            height: 30px;
            margin: 0 15px;
            vertical-align: middle;
        }

        .clear input {
            margin-left: 15px;
            width: 100px;
            height: 30px;
            background-color: cadetblue;
        }

        canvas {
            background-color: gainsboro;
        }
    </style>
</head>

<body>
    <!-- // 清屏,橡皮擦,撤销,显示词汇,画笔颜色,背景颜色,生成图片,分享好友
    https://blog.csdn.net/buppt/article/details/82911003 -->
    <div id="box">
        <nav>
            <div class="changeColor">
                请选择颜色
                <input type="button" style="background-color: pink">
                <input type="button" style="background-color: purple">
                <input type="button" style="background-color: orange">
                <input type="button" style="background-color: green">
                <input type="button" style="background-color: deepskyblue">

                <!-- <input type="color" id="colorBoard" value="colorboard" /> -->
            </div>
            <div class="clear">
                <input type="button" value="清空画布" onclick="clearLayer()" />
                <input type="button" id="eraser" value="橡皮" onclick="eraser()" />
                <input type="button" id="rescind" value="撤消" onclick="rescind()" />
                <input type="range" id="lineRuler" value="线条" min='1' max='30' onclick="handleLine()"/>
            </div>
            <div>
                <!-- 随机显示词汇 药箱 -->
                <input type="button" value="换个题" onclick="changeTopic()"></span> 当前画家:<span id="word"></span>
                <span id="countdown">剩余时间:60</span>s
            </div>
        </nav>
        <canvas width="600" height="400" id="canvas"></canvas>
        <div>
            <input type="button" value="创作完成" onclick="handleComplete()" />
            <input type="button" value="再画一幅" onclick="" />
            <input type="button" value="分享好友" onclick="" />

        </div>

    </div>
    <script>

        var cvs = document.querySelector("canvas");
        var ctx = cvs.getContext("2d");


        var step = -1;
        var canvasHistory = []

        const words = ["苹果", "桌子", "狗", "太阳", "花"];
        const word = words[Math.floor(Math.random() * words.length)];
        document.getElementById("word").innerHTML = word; // 通过 JavaScript 获取该元素并设置其 innerHTML 为变量的值
        cvs.addEventListener("mousedown", function (e) {
            var x = e.clientX - this.offsetLeft;
            var y = e.clientY - this.offsetTop;
            cvs.oldPoint = {
                x: x - 1,
                y: y - 1,
            }
            drawLine(x, y);
            this.addEventListener("mousemove", move);
            this.addEventListener("mouseup", up);

        })

        let countdown = 60;

        // 倒计时
        function updateCountdown() {
            countdown--;
            document.getElementById('countdown').innerText = countdown;

            if (countdown == 0) {
                clearTimeout(timer);
                document.getElementById('countdown').innerText = "时间到!";
            } else {
                timer = setTimeout(updateCountdown, 1000);
            }
        }

        updateCountdown();

        function move(e) {

            var x = e.clientX - this.offsetLeft;

            var y = e.clientY - this.offsetTop;

            drawLine(x, y);

            cvs.oldPoint = {
                x: x,
                y: y,
            }
        }

        function up(e) {
            this.removeEventListener("mousemove", move);
            this.removeEventListener("mouseup", up);
        }


        function drawLine(x, y) {
            ctx.beginPath();
            step++;
            if (step < canvasHistory.length) {
                canvasHistory.length = step
            }
            canvasHistory.push(cvs.toDataURL())
            ctx.lineWidth = 5;
            ctx.lineJoin = "round";
            ctx.lineCap = "round";
            ctx.moveTo(cvs.oldPoint.x, cvs.oldPoint.y);
            ctx.lineTo(x, y);
            ctx.stroke();
            ctx.closePath();
        };

        function clearLayer() {
            ctx.clearRect(0, 0, cvs.width, cvs.height);
        };

        var colorBtn = document.querySelectorAll(".changeColor input");
        var colorBtnArr = [].slice.call(colorBtn);

        colorBtnArr.forEach(function (item, index) {
            item.onclick = function () {
                changeColor(this);
            }
        });

        function changeColor(btn) {
            ctx.strokeStyle = getComputedStyle(btn).backgroundColor;
        };

        function eraser() {
            ctx.strokeStyle = '#fff';
        }

        function rescind() {
            // https://www.27ka.cn/64847.html
            if (step >= 0) {
                step--;
                ctx.clearRect(0, 0, cvs.width, cvs.height);
                let canvasPic = new Image();
                canvasPic.src = canvasHistory[step];
                canvasPic.addEventListener('load', () => {
                    ctx.drawImage(canvasPic, 0, 0);
                });
            } else {
                console.log('不能再继续撤销了');
            }
        }


        function changeTopic() {
            document.getElementById("word").innerHTML = words[Math.floor(Math.random() * words.length)];
            updateCountdown()
        }

        function handleComplete() {
            var dataURL = canvas.toDataURL();
            // 使用 AJAX 或其他方法将数据发送给服务器进行保存
            // 以下代码将数据保存为 PNG 格式图片
            var link = document.createElement('a');
            link.download = 'canvas.png';
            link.href = dataURL;
            link.click();
        }

        // 线条的粗细
        function handleLine(){

        }
    </script>
</body>

</html>

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-01-24 17:28:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-24 17:28:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-24 17:28:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-24 17:28:04       20 阅读

热门阅读

  1. ThreadLocal

    2024-01-24 17:28:04       38 阅读
  2. 改变图片颜色

    2024-01-24 17:28:04       37 阅读
  3. 牛客周赛26

    2024-01-24 17:28:04       36 阅读
  4. orchestrator介绍3.4 web API 的使用

    2024-01-24 17:28:04       37 阅读
  5. 7.docker进阶

    2024-01-24 17:28:04       27 阅读
  6. 【力扣 206】反转链表 C++题解(链表+头插法)

    2024-01-24 17:28:04       33 阅读
  7. MCS-51指令中符号的意义

    2024-01-24 17:28:04       33 阅读