SOURCE

console 命令行工具 X clear

                    
>
console
var canvas = document.getElementById('canvas')
// 画板大小
canvas.width = 280
canvas.height = 280

var context = canvas.getContext('2d')
// 背景颜色
context.fillStyle = 'black'
context.fillRect(0, 0, canvas.width, canvas.height)

// 线宽提示
var range = document.getElementById('customRange1')
range.oninput = function () {
    this.title = 'lineWidth: ' + this.value
}


var Mouse = { x: 0, y: 0 }
var lastMouse = { x: 0, y: 0 }
var painting = false

canvas.onmousedown = function () {
    painting = !painting
}

canvas.onmousemove = function (e) {
    lastMouse.x = Mouse.x
    lastMouse.y = Mouse.y
    Mouse.x = e.pageX - this.offsetLeft
    Mouse.y = e.pageY - this.offsetTop
    if (painting) {
        /*
        画笔参数:
            linewidth: 线宽
            lineJoin: 线条转角的样式, 'round': 转角是圆头
            lineCap: 线条端点的样式, 'round': 线的端点多出一个圆弧
            strokeStyle: 描边的样式, 'white': 设置描边为白色
        */
        context.lineWidth = range.value
        context.lineJoin = 'round'
        context.lineCap = 'round'
        context.strokeStyle = 'white'

        // 开始绘画
        context.beginPath()
        context.moveTo(lastMouse.x, lastMouse.y);
        context.lineTo(Mouse.x, Mouse.y);
        context.closePath()
        context.stroke()
    }
}

canvas.onmouseup = function () {
    painting = !painting
}

// 下载图片
var downl = document.getElementById('downl')
downl.onclick = function () {
    var canvas = document.getElementById('canvas')
    var a = document.createElement('a')
    a.download = 'canvas'
    a.href = canvas.toDataURL('image/jpeg')
    document.body.appendChild(a)
    a.click()
    document.body.removeChild(a)
}

// 清空画布
var clean = document.getElementById('clean')
clean.onclick = function () {
    context.clearRect(0, 0, canvas.width, canvas.height)
    context.fillStyle = 'black'
    context.fillRect(0, 0, canvas.width, canvas.height)
}
<!DOCTYPE html>

<head>
    <!--Bootstrap-->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>

<body>
    <div class="container text-center mt-5 pt-5">
        <div>
            <input type="range" min="0" max="20" step="1" class="custom-range" id="customRange1" style="width: 200px">
        </div>
        <div class="my-3">
            <canvas id="canvas" class="img-thumbnail"></canvas>
        </div>
        <div>
            <button class="btn btn-success" id="downl">下载</button>
            <button class="btn btn-warning" id="clean">清空</button>
        </div>
    </div>
    <!--Myjs-->
    <script src="index.js"></script>
    <!--Bootstrap-->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</body>

</html>