当前位置:网站首页>Canvas analog input box input
Canvas analog input box input
2022-07-28 00:59:00 【Jedi Hongbin】

function :
The insertion point flashes when input stops Insert point update on input
<!DOCTYPE html>
<html lang="zn-ch">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style> canvas {
border: 1px solid #51f; } </style>
</head>
<body>
<!-- <canvas width="500" height="500"></canvas> -->
<script> const canvas = document.createElement("canvas"); document.body.appendChild(canvas); const width = 700; const height = 500; const fontSize = 18; canvas.width = width; canvas.height = height; const ctx = canvas.getContext("2d"); const point = [0, height / 2]; let inputs = [""]; class InterPoint {
timer; prevInterPoint = [0, 0]; lineColor = '#51f'; constructor(ctx, lineWidth, lineHength, delay) {
this.ctx = ctx; this.lineHength = lineHength; this.lineWidth = lineWidth; this.delay = delay; this.drawInsetPoint(true); } /** * Update line color */ setLineColor(lineColor) {
this.lineColor = lineColor; } /** * Draw a vertical line */ drawVerticalLine = (x, y, height) => {
this.ctx.beginPath(); this.ctx.lineWidth = this.lineWidth; this.ctx.moveTo(x, y); this.ctx.lineTo(x, y + height); this.ctx.strokeStyle = this.lineColor; this.ctx.stroke(); this.ctx.closePath(); } /** * Update the coordinates of the insertion point */ updateInterPoint() {
const x = this.ctx.measureText(inputs[inputs.length - 1]).width + 4; const y = (inputs.length - 1) * this.lineHength + height / 2; this.prevInterPoint = [x, y] } /** * Clear the insertion point */ clear() {
this.ctx.clearRect(this.prevInterPoint[0] - this.lineWidth / 2 - 1, this.prevInterPoint[1], this.prevInterPoint[0] + this.lineWidth, this.prevInterPoint[1] + this.lineHength); } /** * loop Draw insertion point Add flashing behavior */ drawInsetPoint(state) {
if (state) {
this.updateInterPoint(); this.drawVerticalLine(...this.prevInterPoint, this.lineHength); } else {
this.clear(); } this.timer = setTimeout(() => {
this.drawInsetPoint(!state); }, this.delay); } /** * Update now */ immediatelyUpdate() {
clearTimeout(this.timer); this.drawInsetPoint(true); } } const interPoint = new InterPoint(ctx, 5, fontSize, 500); function render() {
// Empty the canvas ctx.clearRect(0, 0, width, height); // Draw text inputs.forEach((input, index) => {
drawText(input, 0, index * fontSize + height / 2); }); interPoint.immediatelyUpdate(); } const drawText = (text, x, y) => {
ctx.beginPath(); ctx.font = `${
fontSize}px Microsoft YaHei `; ctx.fillStyle = "#000"; ctx.textBaseline = "top"; ctx.fillText(text, x, y); ctx.closePath(); }; function handleDrawText(text) {
const tempStr = inputs[inputs.length - 1] + text; const tempWidth = ctx.measureText(tempStr).width; if (tempWidth > width) {
// I can't put down a line inputs.push(text); } else inputs[inputs.length - 1] += text; render(); } function delText() {
inputs[inputs.length - 1] = inputs[inputs.length - 1].substr(0, inputs[inputs.length - 1].length - 1) render(); } window.addEventListener("keyup", e => {
if (e.key === "Backspace") {
delText(); } else handleDrawText(e.key); }); </script>
</body>
</html>
边栏推荐
- Basic operations of MySQL database (I) --- Based on Database
- 【OpenCV 例程 300篇】241. 尺度不变特征变换(SIFT)
- Rational and perceptual activities and required skills in programmers' work
- C type use of reflection
- 大众中国豪掷80亿,成国轩高科第一大股东
- scrollview、tableView嵌套解决方案
- Valued at $36billion! SpaceX, which is about to launch its first manned launch, raised $346million
- 分支和循环语句题目练习
- Volkswagen China invested 8billion yuan and became the largest shareholder of GuoXuan high tech
- Solve maze problem recursively
猜你喜欢
![Jerry, if you turn on Bluetooth again, one for two. When the mobile phone is connected to the prototype, it will appear and cannot be connected [chapter]](/img/6c/d4a45981a7fc87f6a82a91017f8ce8.png)
Jerry, if you turn on Bluetooth again, one for two. When the mobile phone is connected to the prototype, it will appear and cannot be connected [chapter]

数组相关知识

融云 IM & RTC 能力上新盘点

SRv6初登场

How to smoothly go online after MySQL table splitting?

Red team killer behinder_ V4.0 (ice scorpion 4.0)

网络安全漏洞分析与漏洞复现

"C language" deep entry rounding & four functions
![论文赏析[ICLR18]联合句法和词汇学习的神经语言模型](/img/1c/5b9726b16f67dfc2016a0c2035baae.png)
论文赏析[ICLR18]联合句法和词汇学习的神经语言模型

《KMP复习 + AC自动机》前传
随机推荐
网络设备硬核技术内幕 防火墙与安全网关篇 (九) 虚拟化神器 (下)
Network device hard core technology insider firewall and security gateway (VII) virtualization artifact (Part 1)
Rancher2.6 monitoring grafana docking LDAP
Add a picture in front of the cell
论文赏析[ICLR18]联合句法和词汇学习的神经语言模型
Sign up now | cloud native technology exchange meetup Guangzhou station has been opened, and I will meet you on August 6!
推荐系统-模型:wide&deep 模型
Wavelet transform learning notes
From the second floor to the third floor
福特SUV版“野马”正式下线,安全、舒适一个不落
Redis-事务与乐观锁
Logic of automatic reasoning 07 - predicate calculus
Arm发布全新A78/G78/N78内核!还有支持自定义的Cortex-X系列CPU
[BuildRelease Management]Parabuild
Ink wheel salon | Li Wenjie, Peking University: a graph database system for knowledge atlas application gstore
网络设备硬核技术内幕 防火墙与安全网关篇 (五) 安全双修大法 中
Read cmake in one article
网络安全漏洞分析与漏洞复现
Buildforge materials
Recurrence of fastjson historical vulnerabilities