Two ways are: dragging the normal label position or dragging the text box position in the canvas
1. Implement mouse dragging of labels Element to any position
css code
#range { position: relative; width: 600px; height: 400px; margin: 10px; background-color: rgb(133, 246, 250); } .icon { position: absolute; height: 100px; width: 100px; cursor: move; background-color: #ff9204; user-select: none; }
html code
<p id="range"> <p class="icon">100*100</p> </p>
js code
const main = document.getElementById('range'); const icon = document.querySelector('.icon'); let move = false; let deltaLeft = 0, deltaTop = 0; // 拖动开始事件,要绑定在被移动元素上 icon.addEventListener('mousedown', function (e) { /* * @des deltaLeft 即移动过程中不变的值 */ deltaLeft = e.clientX-e.target.offsetLeft; deltaTop = e.clientY-e.target.offsetTop; move = true; }) // 移动触发事件要放在,区域控制元素上 main.addEventListener('mousemove', function (e) { if (move) { const cx = e.clientX; const cy = e.clientY; /** 相减即可得到相对于父元素移动的位置 */ let dx = cx - deltaLeft let dy = cy - deltaTop /** 防止超出父元素范围 */ if (dx < 0) dx = 0 if (dy < 0) dy = 0 if (dx > 500) dx = 500 if (dy > 300) dy = 300 icon.setAttribute('style', `left:${dx}px;top:${dy}px`) } }) // 拖动结束触发要放在,区域控制元素上 main.addEventListener('mouseup', function (e) { move = false; console.log('mouseup'); })
2 .Canvas draws a text box and drags it to any position with the mouse
css code
.cus-canvas{ background: rgb(50, 204, 243); } .input-ele{ display: none; position: fixed; width: 180px; border: 0; background-color: #fff; }
html code
<p> <canvas id="canvas" class="cus-canvas" width="800" height="600"></canvas> <input id="inputEle" class="input-ele"/> </p>
js code
The implementation principle is to record the position of the mouse movement and continuously redraw the rectangular frame and text content
let mouseDown = false; let deltaX = 0; let deltaY = 0; let text = 'hello' const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); const cw = canvas.width, ch = canvas.height; const rect = { x: 20, y: 20, width: 150, height: 50 } /** 设置文字和边框样式 */ ctx.font="16px Arial"; ctx.fillStyle = "#fff"; /** 设置为 center 时,文字段的中心会在 fillText的 x 点 */ ctx.textAlign = 'center'; ctx.lineWidth = '2'; ctx.strokeStyle = '#fff'; strokeRect() const inputEle = document.getElementById('inputEle'); inputEle.onkeyup = function(e) { if(e.keyCode === 13) { text = inputEle.value strokeRect() inputEle.setAttribute('style', `display:none`) } } canvas.ondblclick = function(e){ inputEle.setAttribute('style', `left:${e.clientX}px;top:${e.clientY}px;display:block`); inputEle.focus(); } canvas.onmousedown = function(e){ /** 获取视口左边界与canvas左边界的距离 加上 鼠标点击位置与canvas左边界的长度,这个值是相对移动过程中不变的值 */ deltaX=e.clientX - rect.x; deltaY=e.clientY - rect.y; mouseDown = true }; const judgeW = cw-rect.width, judgeH = ch-rect.height; canvas.onmousemove = function(e){ if(mouseDown) { /** 相减即可获得矩形左边界与canvas左边界之间的长度 */ let dx = e.clientX-deltaX; let dy = e.clientY-deltaY; if(dx < 0) { dx = 0; } else if (dx > judgeW) { dx = judgeW; } if(dy < 0) { dy = 0; } else if(dy > judgeH) { dy = judgeH; } rect.x = dx; rect.y = dy; strokeRect() } }; canvas.onmouseup = function(e){ mouseDown = false }; /** 清除指定区域 */ function clearRect() { ctx.clearRect(0, 0, cw, ch) } /** 画矩形 */ function strokeRect() { clearRect() /** 这里如果不调用 beginPath 历史的矩形会重新被绘制 */ ctx.beginPath() ctx.rect(rect.x, rect.y, rect.width, rect.height) ctx.stroke(); // 设置字体内容,以及在画布上的位置 ctx.fillText(text, rect.x + 70, rect.y + 30); }
Recommended tutorial: "HTML Tutorial"
The above is the detailed content of HTML realizes dragging content position at will. For more information, please follow other related articles on the PHP Chinese website!

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools