鱼C论坛

 找回密码
 立即注册
查看: 2387|回复: 6

[作品展示] 【Web】画图

[复制链接]
发表于 2022-2-5 13:14:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 ckblt 于 2022-2-5 13:11 编辑

我开发了一款画图软件,他的界面是这样的:
Snipaste_2022-02-05_12-57-17.png

好丑……

温馨提示
这款画图软件的画布用的是Canvas,如果你的浏览器不支持Canvas请换个浏览器

废话少说,上代码
  1. <!DOCTYPE html>
  2. <html lang="zh-cn">
  3.   <head>
  4.     <meta charset="UTF-8" />
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7.     <title>画图</title>

  8.     <style>
  9.       * {
  10.         margin: 0;
  11.         padding: 0;
  12.       }

  13.       body {
  14.         display: flex;
  15.         justify-content: center;
  16.         align-items: center;
  17.         min-height: 100vh;
  18.         flex-direction: column;
  19.       }

  20.       #picture {
  21.         width: 400px;
  22.         height: 400px;
  23.         border: solid 1px #000;
  24.       }

  25.       #picture canvas {
  26.         width: 100%;
  27.         height: 100%;
  28.       }

  29.       #color,
  30.       #line-width,
  31.       #pen,
  32.       #production {
  33.         margin-bottom: 10px;
  34.       }

  35.       #production {
  36.         text-align: end;
  37.       }
  38.       #production h1 {
  39.         font-size: 2.5rem;
  40.       }
  41.       #production p {
  42.         font-size: 0.5rem;
  43.       }

  44.       /* 本作品未得作者允许禁止转载 Ckblt */
  45.     </style>
  46.   </head>

  47.   <body>
  48.     <div id="production">
  49.       <h1>画图</h1>
  50.       <p>By Ckblt</p>
  51.     </div>
  52.     <div id="color">颜色: <input type="color" /></div>
  53.     <div id="line-width">线条粗细: <input type="range" min="3" max="50" /></div>
  54.     <div id="pen">
  55.       <input type="radio" name="pen" value="pen" checked />
  56.       <label>笔</label>

  57.       <input type="radio" name="pen" value="eraser" />
  58.       <label>橡皮</label>
  59.     </div>

  60.     <div id="picture">
  61.       <canvas width="400" height="400"></canvas>
  62.     </div>
  63.     <script>
  64.       let mousedown = false
  65.       let mouseXY = [0, 0]
  66.       let picture = []
  67.       let color = ''
  68.       let lineWidth = 10
  69.       let temp = []
  70.       let pen = 'pen'
  71.       const ctx = document.querySelector('#picture canvas').getContext('2d')
  72.       let i = 0

  73.       document.querySelector('#picture').addEventListener('mousedown', (ev) => {
  74.         temp = []
  75.         mousedown = true

  76.         ctx.moveTo(...mouseXY)
  77.         ctx.beginPath()
  78.       })
  79.       document.querySelector('#picture').addEventListener('mousemove', (ev) => {
  80.         if (mousedown && i >= 1) {
  81.           mouseXY = [ev.offsetX, ev.offsetY]
  82.           temp.push(mouseXY)

  83.           color = document.querySelector('#color input').value
  84.           lineWidth = document.querySelector('#line-width input').value

  85.           ctx.lineTo(...mouseXY)

  86.           ctx.strokeStyle = pen === 'eraser' ? '#fff' : color
  87.           ctx.lineWidth = lineWidth
  88.           ctx.stroke()
  89.           i -= 3
  90.         }
  91.         i++
  92.       })
  93.       window.addEventListener('mouseup', () => {
  94.         if (mousedown) {
  95.           mousedown = false
  96.           picture.push({
  97.             color: color,
  98.             lineWidth: lineWidth,
  99.             pen: pen,
  100.             content: temp
  101.           })

  102.           ctx.clearRect(0, 0, 400, 400)

  103.           picture.forEach((t) => {
  104.             const c = t.content
  105.             if (c.length != 0) {
  106.               ctx.beginPath()
  107.               ctx.moveTo(...c[0])
  108.               c.forEach((e) => {
  109.                 ctx.lineTo(...e)
  110.               })

  111.               ctx.strokeStyle = t.pen === 'eraser' ? '#fff' : t.color
  112.               ctx.lineWidth = t.lineWidth
  113.               ctx.stroke()
  114.             }
  115.           })
  116.         }
  117.       })

  118.       document.getElementsByName('pen').forEach((e) => {
  119.         e.addEventListener('click', (ev) => {
  120.           pen = e.value
  121.         })
  122.       })

  123.       // 本作品未得作者允许禁止转载 Ckblt
  124.     </script>
  125.     <!-- 本作品未得作者允许禁止转载 Ckblt -->
  126.   </body>
  127. </html>
复制代码


还是那句话:如果有问题,请回复我哟~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-5 17:55:52 | 显示全部楼层
学习了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-5 21:39:20 From FishC Mobile | 显示全部楼层
楼主,代码是什么编程语言
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-5 22:20:37 | 显示全部楼层
shiyouroc 发表于 2022-2-5 21:39
楼主,代码是什么编程语言

html + css + js
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

发表于 2022-2-6 00:25:29 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-7 16:20:51 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-2-7 22:39:47 | 显示全部楼层
shiyouroc 发表于 2022-2-5 21:39
楼主,代码是什么编程语言

后缀名是.html
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-19 16:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表