Jquery的Canvas交互式表格示例

  • <head> 中添加了 CSS 样式定义,包括 #inputbox 的样式。
  • 创建了一个 <div id="container"> 来包含 Canvas 和我们将要添加的单元格元素。
  • #inputbox 样式设置为半透明黄色背景和黑色边框

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Canvas 交互式表格示例 - 带自定义右键菜单</title>
    <style>
        #container {
            position: relative;
            width: 300px;
            height: 300px;
        }
        #myCanvas {
            position: absolute;
            top: 0;
            left: 0;
            z-index: 1;
        }
        .cell {
            position: absolute;
            width: 100px;
            height: 100px;
            z-index: 2;
            box-sizing: border-box;
        }
        #inputbox {
            background-color: rgba(255, 255, 0, 0.5);
            border: 2px solid #000;
        }
        #contextMenu {
            display: none;
            position: absolute;
            background-color: #f9f9f9;
            border: 1px solid #ccc;
            padding: 5px 0;
            z-index: 1000;
        }
        #contextMenu div {
            padding: 5px 20px;
            cursor: pointer;
        }
        #contextMenu div:hover {
            background-color: #f1f1f1;
        }
    </style>
</head>
<body>
    <div id="container">
        <canvas id="myCanvas" width="300" height="300"></canvas>
    </div>
    <div id="contextMenu">
        <div id="copy">复制</div>
        <div id="paste">粘贴</div>
    </div>

    <script>
        const canvas = document.getElementById('myCanvas');
        const ctx = canvas.getContext('2d');
        const container = document.getElementById('container');
        const contextMenu = document.getElementById('contextMenu');
        
        // 设置表格参数
        const rows = 3;
        const cols = 3;
        const cellWidth = 100;
        const cellHeight = 100;

        let copiedContent = null;

        // 绘制表格
        function drawGrid() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            
            for (let i = 0; i <= rows; i++) {
                ctx.beginPath();
                ctx.moveTo(0, i * cellHeight);
                ctx.lineTo(cols * cellWidth, i * cellHeight);
                ctx.stroke();
            }
            for (let j = 0; j <= cols; j++) {
                ctx.beginPath();
                ctx.moveTo(j * cellWidth, 0);
                ctx.lineTo(j * cellWidth, rows * cellHeight);
                ctx.stroke();
            }
        }

        // 处理左键点击事件
        canvas.addEventListener('click', (event) => {
            const rect = canvas.getBoundingClientRect();
            const x = event.clientX - rect.left;
            const y = event.clientY - rect.top;
            
            const col = Math.floor(x / cellWidth);
            const row = Math.floor(y / cellHeight);
            
            if (row < rows && col < cols) {
                const oldInputBox = document.querySelector('#inputbox');
                if (oldInputBox) {
                    oldInputBox.remove();
                }

                const inputBox = document.createElement('div');
                inputBox.id = 'inputbox';
                inputBox.className = 'cell';
                inputBox.style.left = `${col * cellWidth}px`;
                inputBox.style.top = `${row * cellHeight}px`;

                container.appendChild(inputBox);
            }
        });

        // 处理右键点击事件
        canvas.addEventListener('contextmenu', (event) => {
            event.preventDefault();
            const rect = canvas.getBoundingClientRect();
            const x = event.clientX - rect.left;
            const y = event.clientY - rect.top;
            
            contextMenu.style.display = 'block';
            contextMenu.style.left = `${event.clientX}px`;
            contextMenu.style.top = `${event.clientY}px`;
        });

        // 处理复制和粘贴
        document.getElementById('copy').addEventListener('click', () => {
            const inputBox = document.querySelector('#inputbox');
            if (inputBox) {
                copiedContent = inputBox.innerHTML;
            }
            contextMenu.style.display = 'none';
        });

        document.getElementById('paste').addEventListener('click', () => {
            const inputBox = document.querySelector('#inputbox');
            if (inputBox && copiedContent) {
                inputBox.innerHTML = copiedContent;
            }
            contextMenu.style.display = 'none';
        });

        // 点击其他地方时隐藏右键菜单
        document.addEventListener('click', (event) => {
            if (event.target.closest('#contextMenu') === null) {
                contextMenu.style.display = 'none';
            }
        });

        // 初始绘制
        drawGrid();
    </script>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天天进步2015

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值