Home Web Front-end JS Tutorial How to draw circles and rectangles using JavaScript

How to draw circles and rectangles using JavaScript

Sep 14, 2017 am 11:53 AM
javascript js round

本文实例讲述了js绘制圆形和矩形的方法。分享给大家供大家参考。具体如下:

这里使用js来绘制圆形和矩形,支持选择图形的背景颜色,同时可设置圆角矩形、半径、正圆、矩形、正方形这几个选项。或许这些图形你不需要,但重要的是让你学会JavaScript绘制图形的方法,这是要表达的核心。

运行效果如下图所示:

具体代码如下:

<!doctype html>
<html>
<head>
<title>js来绘制圆形和矩形</title>
<style>
 *{margin:0; padding:0;}
 #p{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("p");
   oR.id="p";
   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>
 <p 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>
 </p>
 <p id="canvas">
 </p>
</body>
</html>
Copy after login

The above is the detailed content of How to draw circles and rectangles using JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to make round pictures and text in ppt How to make round pictures and text in ppt Mar 26, 2024 am 10:23 AM

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 implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

How to make a round picture in ppt How to make a round picture in ppt Mar 25, 2024 pm 03:54 PM

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.

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

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

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

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

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

See all articles