原生js实现画板自由绘画图形文字可拖拽移动擦除导出base64示例代码

代码语言:html

所属分类:其他

代码描述:原生js实现画板自由绘画图形文字可拖拽移动擦除导出base64示例代码,还可本地上传图片转base64发送后端,适合au绘画参考图交互功能的前端开发。

代码标签: 原生 js 画板 自由 绘画 图形 文字 拖拽 移动 擦除 导出 base64 示例 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>UI Drawing and Image Upload</title>
<link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/css/all.6.4.0.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f0f0;
        }
        .container {
            text-align: center;
        }
        #canvas {
            border: 1px solid #ddd;
            background-color: white;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            display: none;
        }
        .controls {
            margin-top: 20px;
        }
        #drawingControls, #uploadControls {
            display: none;
        }
        button {
            background-color: #4CAF50;
            border: none;
            color: white;
            padding: 10px 20px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 4px 2px;
            cursor: pointer;
            border-radius: 4px;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #45a049;
        }
        .icon-button {
            font-size: 24px;
            width: 50px;
            height: 50px;
            border-radius: 50%;
            padding: 0;
            display: inline-flex;
            align-items: center;
            justify-content: center;
        }
        #imageUpload {
            display: none;
        }
        .file-upload-label {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            display: inline-block;
            margin: 4px 2px;
        }
        .file-upload-label:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
    <div class="container">
        <div id="initialChoice">
            <button id="chooseDrawing"><i class="fas fa-paint-brush"></i> 使用画板</button>
            <button id="chooseUpload"><i class="fas fa-upload"></i> 上传本地图片</button>
        </div>

        <canvas id="canvas" width="800" height="600"></canvas>
        
        <div id="drawingControls" class="controls">
            <button id="switchDragMode" class="icon-button"><i class="fas fa-arrows-alt"></i></button>
            <button id="switchDrawMode" class="icon-button"><i class="fas fa-pencil-alt"></i></button>
            <button id="switchEraseMode" class="icon-button"><i class="fas fa-eraser"></i></button>
            <button id="addText" class="icon-button"><i class="fas fa-font"></i></button>
            <button id="confirmDrawing" class="icon-button"><i class="fas fa-check"></i></button>
            <button id="toggleSize" class="icon-button"><i class="fas fa-expand-arrows-alt"></i></button>
        </div>

        <div id="uploadControls" class="controls">
            <label for="imageUpload" class="file-upload-label">
                <i class="fas fa-file-upload"></i> 选择文件
            </label>
            <input type="file" id="imageUpload" accept="image/*">
            <button id="confirmUpload"><i class="fas fa-check"></i> 确认并发送</button>
        </div>
    </div>

    <script>
        const initialChoice = document.getElementById('initialChoice');
        const canvas = document.getElementById('canvas');
        const ctx = canvas.getContext('2d');
        const drawingControls = document.getElementById('drawingControls');
        const uploadControls = document.getElementById('uploadControls');
        
        let isDrawing = false;
        let elements = [];
        let mode = 'draw';
        let currentPath = [];
        let draggedElement = null;
        let dragStartX, dragStartY;
        let isPCSize = true;
        let eraserSize = 20;

        document.getElementById('chooseDrawing').addEventListener('click', initializeDrawing);
        document.getElementById('chooseUpload').addEventListener('click', initializeUpload);
        document.getElementById('switchEraseMode').addEventListener('click', switchEraseMode);
        document.getElementById('switchDragMode').addEventListener('click', switchDragMode);
        document.getElementById('switchDrawMode').addEventListener('click', switchDrawMode);
        document.getElementById('addText').addEventListener('click', addText);
        document.getElementById('confirmDrawing').addEventListener('click', saveCanvas);
        document.getElementById('confirmUpload').addEventListener('click', uploadImage);
        document.getElementById('toggleSize').addEventListener('click', toggleCanvasSize);

        canvas.addEventListener('mousedown', handleMouseDown);
        canvas.addEventListener('mousemove', handleMouseMove);
        canvas.addEventListener('mouseup', handleMouseUp);
        canvas.addEventListener('mouseout', handleMouseUp);

        function initializeDrawing() {
            initialChoice.style.display = 'none';
            canvas.style.display = 'block';
            drawingControls.style.display = 'block';
        }

        function initializeUpload() {
            initialChoice.style.display = 'none';
            uploadControls.style.display = 'block';
        }

        function switchDrawMode() {
            mode = 'draw';
            draggedElement = null;
            redrawCanvas();
        }
        
        function switchDragMode() {
            mode = 'drag';
            draggedElement = null;
            redrawCanvas();
        }

        function switchEraseMode() {
            mode = 'erase';
            draggedElement = null;
            redrawCanvas();
        }

        function toggleCanvasSize() {
            isPCSize = !isPCSize;
            if (isPCSize) {
                canvas.width = 800;
                canvas.height = 6.........完整代码请登录后点击上方下载按钮下载查看

网友评论0