js method of drawing circles and rectangles_javascript skills
The example in this article describes how to draw circles and rectangles in js. Share it with everyone for your reference. The details are as follows:
JS is used here to draw circles and rectangles, and it supports selecting the background color of graphics. At the same time, you can set the options of rounded rectangle, radius, perfect circle, rectangle, and square. Maybe you don't need these graphics, but it is important for you to learn how to draw graphics in JavaScript. This is the core of the expression.
The operation effect is as shown below:
The specific code is as follows:
<!doctype html> <html> <head> <title>js来绘制圆形和矩形</title> <style> *{margin:0; padding:0;} #div{position:absolute; background:#ccc;} .sel{ margin:30px auto; width:960px; overflow:hidden} li{ list-style:none; float:left; width:60px; height:20px;} #colors{ width:500px; float:left} .selColor{ float:left} #radius{ width:40px; height:20px;} .red{background:red;} .yellow{background:yellow;} .blue{background:blue;} .pink{background:pink;} .black{background:black;} .orange{background:orange;} .green{ background:green;} .xz{ width:340px; float:right;} #canvas{ width:960px; height:500px; border:1px solid #ccc; margin:0 auto} </style> <script> function $Id(id) { return document.getElementById(id); } window.onload=function(){ var oCanvas=$Id('canvas'); var oRoud=$Id('roud'); var oRadius=$Id('radius'); var oCir=$Id('circle'); var oSqu=$Id('squ'); var oColors=$Id('colors'); var aColors=oColors.getElementsByTagName('li'); var color='red'; var aInputs=document.getElementsByTagName('input'); var xz='roud'; var arr=[]; for(var i=0;i<aInputs.length;i++) { if(aInputs[i].type=='checkbox') { arr.push(aInputs[i]); } } for(var i=0;i<arr.length;i++) { arr[i].onclick=function() { if(!this.checked) { this.checked=false; } else { for(var j=0;j<arr.length;j++) { arr[j].checked=false; } this.checked=true; xz=this.value; } } } //选择颜色 for(var i=0;i<aColors.length;i++) { aColors[i].onclick=function() { color=this.className; } } oCanvas.onmousedown=function(ev) { if(oCanvas.setCapture) { oCanvas.setCapture(); } for(var i=0;i<arr.length;i++) { if(arr[i].checked) { arr[i].checked=true; xz= arr[i].value; } } var oEv=ev||window.event; var disX=oEv.clientX; var disY=oEv.clientY; var oR=document.createElement('div'); oR.id="div"; oR.style.top=disY+'px'; oR.style.left=disX+'px'; oR.style.backgroundColor=color; document.body.appendChild(oR); document.onmousemove=function(ev) { var oEv=ev||window.event; var x=oEv.clientX; var y=oEv.clientY; if(x<oCanvas.offsetLeft) { x=oCanvas.offsetLeft; } else if(x>oCanvas.offsetLeft+oCanvas.offsetWidth) { x=oCanvas.offsetLeft+oCanvas.offsetWidth } if(y<oCanvas.offsetTop) { y=oCanvas.offsetTop; } else if(y>oCanvas.offsetTop+oCanvas.offsetHeight) { y=oCanvas.offsetTop+oCanvas.offsetHeight } oR.style.width=Math.abs(x-disX)+'px'; oR.style.top=Math.min(disY,y)+'px'; oR.style.left=Math.min(disX,x)+'px'; switch(xz) { case 'roud': oR.style.height=Math.abs(y-disY)+'px'; oR.style.borderRadius=oRadius.value+'px'; break; case 'circle': oR.style.height=Math.min(Math.abs(x-disX),Math.abs(y-disY))+'px'; oR.style.width=Math.min(Math.abs(x-disX),Math.abs(y-disY))+'px'; oR.style.borderRadius=(Math.min(Math.abs(x-disX),Math.abs(y-disY)))/2+'px'; break; case 'squ': oR.style.height=Math.abs(y-disY)+'px'; break; case 'square': oR.style.height=Math.min(Math.abs(x-disX),Math.abs(y-disY))+'px'; oR.style.width=Math.min(Math.abs(x-disX),Math.abs(y-disY))+'px'; } } document.onmouseup=function() { document.onmousemove=null; document.onmouseout=null; if(oCanvas.releaseCapture) { oCanvas.releaseCapture(); } } return false; } } </script> </head> <body> <div class="sel"> <span class="selColor">请选择一种颜色</span> <ul id="colors"> <li value="red" class="red"></li> <li value="yellow" class="yellow"></li> <li value="blue" class="blue"></li> <li value="pink" class="pink"></li> <li value="black" class="black"></li> <li value="orange" class="orange"></li> <li value="green" class="green"></li> </ul> <p class="xz"> <input type="checkbox" value="roud" id="roud" />圆角矩形 <label>半径</label><input type="text" value="" id="radius" /> <input type="checkbox" id="circle" value="circle" />正圆 <input type="checkbox" id="squ" value="squ" />矩形 <input type="checkbox" id="square" value="square" />正方形 </p> </div> <div id="canvas"> </div> </body> </html>
I hope this article will be helpful to everyone’s JavaScript programming design.

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

First, draw a circle in PPT, then insert a text box and enter text content. Finally, set the fill and outline of the text box to None to complete the production of circular pictures and text.

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

How to make a circular picture in ppt: 1. Use the crop function; 2. Use the shape tool; 3. Use shortcut keys and control points to adjust.

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

ppt is widely used in many fields and work, especially in education, architecture, etc. When it comes to architectural ppt, we must first think of the presentation of some architectural drawings. If we do not use professional drawing software, can we directly draw a simple architectural plan? In fact, we can complete the operation here. Below, we will draw a relatively simple floor plan to give you an idea. I hope you can complete better floor plan drawings based on this idea. 1. First, we double-click to open the ppt software on the desktop and click to create a new presentation blank document. 2. We find Insert→Shape→Rectangle in the menu bar. 3. After drawing the rectangle, double-click the graphic and modify the fill color type. Here we can modify

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese
