Canvas绘画之多边形画板,绘制多边形,携带背景图和绘画功能,带有全部清除的功能,用这个

简介: Canvas绘画之多边形画板,绘制多边形,携带背景图和绘画功能,带有全部清除的功能,用这个

源码:

<!DOCTYPE html>
<html lang="zh">
<head>
    <title>多边形画板,绘制多边形</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 
    <style>
        body {
            background: #eeeeee;
        }
 
        #controls {
            position: absolute;
            left: 25px;
            top: 25px;
        }
 
        #canvas {
            background: #ffffff;
            cursor: pointer;
            margin-left: 10px;
            margin-top: 10px;
            -webkit-box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
            -moz-box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
            box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
        }
    </style>
</head>
 
<body>
<canvas id='canvas' width='850' height='600'>
    Canvas not supported
</canvas>
 
<div id='controls'>
    <label for='strokeStyleSelect'> Stroke color:</label><select id='strokeStyleSelect'>
    <option value='red'>red</option>
    <option value='green'>green</option>
    <option value='blue'>blue</option>
    <option value='orange'>orange</option>
    <option value='cornflowerblue' selected>cornflowerblue</option>
    <option value='goldenrod'>goldenrod</option>
    <option value='navy'>navy</option>
    <option value='purple'>purple</option>
</select>
 
    <label for='fillStyleSelect'>Fill color: </label><select id='fillStyleSelect'>
    <option value='rgba(255,0,0,0.5)'>semi-transparent red</option>
    <option value='green'>green</option>
    <option value='rgba(0,0,255,0.5)'>semi-transparent blue</option>
    <option value='orange'>orange</option>
    <option value='rgba(100,140,230,0.5)'>semi-transparent cornflowerblue</option>
    <option value='goldenrod' selected>goldenrod</option>
    <option value='navy'>navy</option>
    <option value='purple'>purple</option>
</select>
 
    <label for='sidesSelect'>Sides: </label><select id='sidesSelect'>
    <option value=3 selected>3</option>
    <option value=4>4</option>
    <option value=5>5</option>
    <option value=6>6</option>
    <option value=7>7</option>
    <option value=8>8</option>
    <option value=9>9</option>
</select>
 
    <label for='startAngleSelect'>Start angle: </label><select id='startAngleSelect'>
    <option value=0 selected>0</option>
    <option value=22.5>22.5</option>
    <option value=45>45</option>
    <option value=67.5>67.5</option>
    <option value=90>90</option>
</select>
 
    <label for='fillCheckbox'>Fill</label><input id='fillCheckbox' type='checkbox' checked/>
    <input id='eraseAllButton' type='button' value='Erase all'/>
</div>
</body>
<script>
    'use strict';
 
    var canvas = document.getElementById('canvas'),
        context = canvas.getContext('2d'),
        eraseAllButton = document.getElementById('eraseAllButton'),
        strokeStyleSelect = document.getElementById('strokeStyleSelect'),
        startAngleSelect = document.getElementById('startAngleSelect'),
 
        fillStyleSelect = document.getElementById('fillStyleSelect'),
        fillCheckbox = document.getElementById('fillCheckbox'),
 
        sidesSelect = document.getElementById('sidesSelect'),
 
        drawingSurfaceImageData,
 
        mousedown = {},
        rubberbandRect = {},
        dragging = false,
 
        sides = 8,
        startAngle = 0,
 
        guidewires = true,
 
        Point = function (x, y) {
            this.x = x;
            this.y = y;
        };
 
 
    // Functions.....................................................
 
    function drawGrid(color, stepx, stepy) {
        context.save()
 
        context.strokeStyle = color;
        context.fillStyle = '#ffffff';
        context.lineWidth = 0.5;
        context.fillRect(0, 0, context.canvas.width, context.canvas.height);
 
        for (var i = stepx + 0.5; i < context.canvas.width; i += stepx) {
            context.beginPath();
            context.moveTo(i, 0);
            context.lineTo(i, context.canvas.height);
            context.stroke();
        }
 
        for (var i = stepy + 0.5; i < context.canvas.height; i += stepy) {
            context.beginPath();
            context.moveTo(0, i);
            context.lineTo(context.canvas.width, i);
            context.stroke();
        }
 
        context.restore();
    }
 
    function windowToCanvas(e) {
        var x = e.x || e.clientX,
            y = e.y || e.clientY,
            bbox = canvas.getBoundingClientRect();
 
        return { x: x - bbox.left * (canvas.width  / bbox.width),
            y: y - bbox.top  * (canvas.height / bbox.height)
        };
    }
 
    // save和restore的服务区域
 
    function saveDrawingSurface() {
        drawingSurfaceImageData = context.getImageData(0, 0,
            canvas.width,
            canvas.height);
    }
 
    function restoreDrawingSurface() {
        context.putImageData(drawingSurfaceImageData, 0, 0);
    }
 
    // Rubberbands...................................................
 
    function updateRubberbandRectangle(loc) {
        rubberbandRect.width = Math.abs(loc.x - mousedown.x);
        rubberbandRect.height = Math.abs(loc.y - mousedown.y);
 
        if (loc.x > mousedown.x) rubberbandRect.left = mousedown.x;
        else                     rubberbandRect.left = loc.x;
 
        if (loc.y > mousedown.y) rubberbandRect.top = mousedown.y;
        else                     rubberbandRect.top = loc.y;
    }
 
    function getPolygonPoints(centerX, centerY, radius, sides, startAngle) {
        var points = [],
            angle = startAngle || 0;
 
        for (var i=0; i < sides; ++i) {
            points.push(new Point(centerX + radius * Math.sin(angle),
                centerY - radius * Math.cos(angle)));
            angle += 2*Math.PI/sides;
        }
 
        return points;
    }
 
    function createPolygonPath(centerX, centerY, radius, sides, startAngle) {
        var points = getPolygonPoints(centerX, centerY, radius, sides, startAngle);
 
        context.beginPath();
 
        context.moveTo(points[0].x, points[0].y);
 
        for (var i=1; i < sides; ++i) {
            context.lineTo(points[i].x, points[i].y);
        }
 
        context.closePath();
    }
 
    function drawRubberbandShape(loc, sides, startAngle) {
        createPolygonPath(mousedown.x, mousedown.y,
            rubberbandRect.width,
            parseInt(sidesSelect.value),
            (Math.PI / 180) * parseInt(startAngleSelect.value));
        context.stroke();
 
        if (fillCheckbox.checked) {
            context.fill();
        }
    }
 
    function updateRubberband(loc, sides, startAngle) {
        updateRubberbandRectangle(loc);
        drawRubberbandShape(loc, sides, startAngle);
    }
 
    // Guidewires....................................................
 
    function drawHorizontalLine (y) {
        context.beginPath();
        context.moveTo(0,y+0.5);
        context.lineTo(context.canvas.width,y+0.5);
        context.stroke();
    }
 
    function drawVerticalLine (x) {
        context.beginPath();
        context.moveTo(x+0.5,0);
        context.lineTo(x+0.5,context.canvas.height);
        context.stroke();
    }
 
    function drawGuidewires(x, y) {
        context.save();
        context.strokeStyle = 'rgba(0,0,230,0.4)';
        context.lineWidth = 0.5;
        drawVerticalLine(x);
        drawHorizontalLine(y);
        context.restore();
    }
 
    // Event handlers................................................
 
    canvas.onmousedown = function (e) {
        var loc = windowToCanvas(e);
 
        saveDrawingSurface();
 
        e.preventDefault();
 
        saveDrawingSurface();
        mousedown.x = loc.x;
        mousedown.y = loc.y;
        dragging = true;
    };
 
    canvas.onmousemove = function (e) {
        var loc;
 
        if (dragging) {
            e.preventDefault();
 
            loc = windowToCanvas(e);
            restoreDrawingSurface();
            updateRubberband(loc, sides, startAngle);
 
            if (guidewires) {
                drawGuidewires(mousedown.x, mousedown.y);
            }
        }
    };
 
    canvas.onmouseup = function (e) {
        var loc = windowToCanvas(e);
        dragging = false;
        restoreDrawingSurface();
        updateRubberband(loc);
    };
 
    eraseAllButton.onclick = function (e) {
        context.clearRect(0, 0, canvas.width, canvas.height);
        drawGrid('lightgray', 10, 10);
        saveDrawingSurface();
    };
 
    strokeStyleSelect.onchange = function (e) {
        context.strokeStyle = strokeStyleSelect.value;
    };
 
    fillStyleSelect.onchange = function (e) {
        context.fillStyle = fillStyleSelect.value;
    };
 
    // 初始化
 
    context.strokeStyle = strokeStyleSelect.value;
    context.fillStyle = fillStyleSelect.value;
    drawGrid('lightgray', 10, 10);
</script>
</html>
相关文章
|
3月前
|
前端开发
Canvas绘画之文本缩放并旋转
Canvas绘画之文本缩放并旋转
|
3月前
|
机器学习/深度学习 前端开发 算法
canvas系列教程03 —— 线的样式、绘制文本、操作图片(图片的渲染、缩放、裁剪、切割、平铺、特效)、变换元素(平移、缩放、旋转)(二)
canvas系列教程03 —— 线的样式、绘制文本、操作图片(图片的渲染、缩放、裁剪、切割、平铺、特效)、变换元素(平移、缩放、旋转)(二)
34 0
|
3月前
|
存储 前端开发 JavaScript
canvas系列教程03 —— 线的样式、绘制文本、操作图片(图片的渲染、缩放、裁剪、切割、平铺、特效)、变换元素(平移、缩放、旋转)(一)
canvas系列教程03 —— 线的样式、绘制文本、操作图片(图片的渲染、缩放、裁剪、切割、平铺、特效)、变换元素(平移、缩放、旋转)(一)
156 0
|
3月前
|
Web App开发 前端开发
canvas系列教程04 —— 渐变、阴影、路径、状态、Canvas对象、图形重叠模式
canvas系列教程04 —— 渐变、阴影、路径、状态、Canvas对象、图形重叠模式
213 0
|
10月前
|
计算机视觉 Python
OpenCV中绘制文字和运动的小球动画
要在OpenCV中绘制文字和运动的小球动画,可以使用以下方法: 1. 导入所需库:
111 8
|
12月前
cesium中绘制立方体、设置材质、操作相机及获取鼠标经纬度和高度的方法
cesium中绘制立方体、设置材质、操作相机及获取鼠标经纬度和高度的方法
209 0
|
12月前
|
存储 前端开发
canvas自定义绘制顺序解决遮挡问题
canvas自定义绘制顺序解决遮挡问题
205 0
|
C# 图形学
C#绘制自定义小人
C#绘制自定义小人
SwiftUI—通过Path路径绘制不规则的线条和图形
SwiftUI—通过Path路径绘制不规则的线条和图形
616 0
SwiftUI—通过Path路径绘制不规则的线条和图形
|
移动开发 缓存 前端开发
H5画布 canvas(二)绘制文字、图片、坐标系,canvas颜色和样式,canvas绘制环境
H5画布 canvas(二)绘制文字、图片、坐标系,canvas颜色和样式,canvas绘制环境
596 0
H5画布 canvas(二)绘制文字、图片、坐标系,canvas颜色和样式,canvas绘制环境