当前位置:网站首页>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>
边栏推荐
- Selection of FFT sampling frequency and sampling points
- 《KMP复习 + AC自动机》前传
- 网络设备硬核技术内幕 防火墙与安全网关篇 (六) 安全双修大法 下
- Multithreading and multithreaded programming
- Redis learning and understanding of three special data types
- Can TSMC Samsung build a production line without American equipment for Huawei?
- 融云 IM & RTC 能力上新盘点
- Interesting Huffman tree
- Ink wheel salon | Li Wenjie, Peking University: a graph database system for knowledge atlas application gstore
- DC motor winding parameters
猜你喜欢
![[BuildRelease Management]Parabuild](/img/80/11c2b539c217ecd6ba55668d3e71e9.png)
[BuildRelease Management]Parabuild

Resolved Unicode decodeerror: 'UTF-8' codec can't decode byte 0xa1 in position 0: invalid start byte

Branch and loop sentence exercises

KMP review + AC automata Prequel

Set 数据构造函数

One year anniversary of creation, Chongba young Lang

map集合

What is the org relationship mitigation strategy of Microsoft edge browser tracking prevention

leetcode:1997. 访问完所有房间的第一天【跳跃dp】

Buildforge materials
随机推荐
Add a picture in front of the cell
Srv6 debut
110. SAP UI5 FileUploader 控件深入介绍 - 为什么需要一个隐藏的 iframe
One year anniversary of creation, Chongba young Lang
立即报名 | 云原生技术交流 Meetup 广州站已开启,8 月 6 号与你相遇!
Arm发布全新A78/G78/N78内核!还有支持自定义的Cortex-X系列CPU
Array related knowledge
Basic operations of MySQL database (I) --- Based on Database
Leetcode 452. minimum number of arrows to burst balloons (medium)
R language uses ggplot2 visualization: use ggpattern package to add custom stripe patterns, shadows, stripes, or other patterns or textures to the grouped bar graph
Logic of automatic reasoning 09 - automatic theorem proving
Confused SCM matrix keys
数组相关知识
Leetcode:1997. the first day after visiting all rooms [jump DP]
Programmer growth Chapter 30: do you really understand feedback?
Point divide and conquer analysis
Focus on demand flow rather than idle people
网络设备硬核技术内幕 防火墙与安全网关篇 (十)
Resolved Unicode decodeerror: 'UTF-8' codec can't decode byte 0xa1 in position 0: invalid start byte
红队大杀器 Behinder_v4.0(冰蝎4.0)