当前位置: 首页 > news >正文

js实现填涂画板

文章目录

  • 1实现效果
  • 2 实现代码

凑个数,存粹是好玩儿,哈哈...

1实现效果

在这里插入图片描述

最上方一栏:
左侧是颜色按钮,点击选中颜色,
中间是功能按钮,重置颜色、清空画板、回退、涂改液(填涂色置为白色)
右侧是显示当前所选颜色

下方填涂板是表格实现的。

2 实现代码

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>画图板</title><style>* {margin: 0px;padding: 0px;}#bt {display: flex;margin-top: 5px;height: 30px;box-sizing: border-box;}.bt1 {padding-left: 20px;flex: 1;box-sizing: border-box;}.bt2 {flex: 1;text-align: center;}.bt3 {flex: 1;font-size: 20px;}#content {margin-top: 10px;}table {border-collapse: collapse;}td {width: 25px;height: 25px;border: 0.5px solid gray;}td:hover {background-color: gray;}.b1 {margin-left: 10px;width: 15px;height: 15px;}.b2 {margin-right: 10px;width: 100px;line-height: 27px;background-color: lightgray;border: none;border-radius: 10px;}.b1:hover {width: 20px;height: 20px;}.b2:hover {background-color: lightblue;}#colorName {display: inline-block;color: red;width: 200px;height: 30px;text-align: center;background-color: gray;}</style>
</head><body><div id="bt"><div class="bt1"></div><div class="bt2"><button class="b2">重置颜色</button><button class="b2">清空画板</button><button class="b2">后退一步</button><button class="b2">涂改液</button></div><div class="bt3">当前所选颜色为:<span id="colorName">rgb(255,0,0)</span></div></div><div id="content"></div><script>// 存放改变颜色的td索引let bakeTable = [];let div = document.getElementById('content');let width = 60;let height = 25;// 初始化颜色按钮createColorTable();// 打印画板let str = "<table>"for (let i = 0; i < height; i++) {str += "<tr>"for (let j = 0; j < width; j++) {str += "<td></td>";}str += '</tr>';}str += "</table>"div.innerHTML = str;// 给td加事件let tds = document.querySelectorAll("td");tds.forEach((td,i) => {td.onclick = setColor.bind(td,null,i);});// 获取功能按钮let buts_2 = document.querySelectorAll('.b2');// 获取颜色名称let tdColorName = document.querySelector("#colorName");// 初始化颜色let butColor = 'rgb(255,0,0)';// 按钮功能buts_2[0].onclick = createColorTable;buts_2[1].onclick = clearCanvas;buts_2[2].onclick = backStep;buts_2[3].onclick = function(){butColor = 'rgb(255, 255, 255)';tdColorName.innerHTML = butColor;tdColorName.style.color = butColor;};function getColor(but) {but = this;butColor = but.style.backgroundColor;tdColorName.innerHTML = butColor;tdColorName.style.color = butColor;}function setColor(td,i) {td = this;td.style.backgroundColor = butColor;bakeTable.push(i);}function randomColor() {let color = "#";let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F'];for (let i = 0; i < 6; i++) {let index = parseInt(Math.random() * arr.length);color += arr[index];}return color;}function createColorTable() {let btn_str = '';for (let i = 0; i < 15; i++) {btn_str += '<button class="b1" style="background-color: ' + randomColor() + '"></button>';}document.querySelector('.bt1').innerHTML = btn_str;let buts = document.querySelectorAll('.b1');buts.forEach(but => {but.onclick = getColor;})}function clearCanvas(){document.querySelectorAll("td").forEach(td=>{td.style.backgroundColor = "";})}function backStep(){if(bakeTable.length>0){let i = bakeTable.pop();document.querySelectorAll("td")[i].style.backgroundColor = "";}}</script>
</body></html>
http://www.lryc.cn/news/287778.html

相关文章:

  • springboot农机电招平台源码和论文
  • TensorFlow 深度学习 开发环境搭建 全教程
  • Qt —— QCharts之曲线示波器(附源码)
  • 【秒剪】如何更换视频画幅比例以及画面背景?
  • HarmonyOS鸿蒙学习笔记(23)监听Wifi状态变化
  • mac 安装配置oh-my-zsh
  • [pytorch入门] 2. tensorboard
  • 基于卡尔曼滤波的平面轨迹优化
  • GBASE南大通用分享如何更新嵌套的集合
  • Maya------插入循环边
  • Nginx_入门
  • 坚持刷题 | 平衡二叉树
  • 江大白 | 万字长文图解Numpy教程,看这一篇就够了!
  • 数据结构——静态链表
  • C++ 知识列表【图】
  • 系统登录的时候的密码如何做到以加密的形式进行登录【java.security包下的api】工具类。
  • java基础学习: 什么是泛型的类型擦除
  • Vue+OpenLayers7入门到实战:在地图上添加缩放控件、比例尺控件和鼠标经纬度位置显示控件
  • 极简生活|可以慢慢变富的8个习惯
  • MySQL基础(一)
  • 【Linux编译器-gcc/g++使用】
  • SQL提示与索引终章
  • 基于OpenSSL的SSL/TLS加密套件全解析
  • 01-echarts如何绘制三维折线图
  • Linux-共享内存
  • 深入分析 Linux 网络丢包问题
  • web安全学习笔记【08】——算法1
  • 2024最新版Python 3.12.1安装使用指南
  • Oracle 经典练习题 50 题
  • PyTorch的衍生资源