当前位置:网站首页>Canvas drawing (mouse click to draw and lift to end)
Canvas drawing (mouse click to draw and lift to end)
2022-07-24 02:06:00 【(-^_^-)】



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* The second step : Set canvas style */
#myCanvas{
border: 5px solid black;
background-color: pink;
}
</style>
</head>
<body>
<!-- First step : Define canvas Width height without px-->
<canvas id="myCanvas" width="860" height="480"> Prompt information : Browsers do not support canvases </canvas>
<!-- The second step : Set canvas style css-->
<!-- The third step :JS Preparing the drawing context ----- paint brush -->
<!-- Step four :JS mapping -->
<script src="js/1.js">
</script>
</body>
</html>
// Set canvas variables 、 Brush variables
var canvas,pen;
// The third step :JS Preparing the drawing context ----- paint brush
var canvas=document.getElementById("myCanvas");//2d Represents the current drawing 2 Dimensional plane drawing paint brush
var pen = canvas.getContext("2d"); //getContext Get context
pen.lineWidth=5;// Pen thickness
pen.strokeStyle ="black";// The color of the pen
// Consider what actions the drawing is completed in
// Mouse operation has three states The mouse click ( Ready to start drawing ) The mouse is raised ( Finish drawing ) Press and move the mouse ( Start drawing )
var mousePress=false; // Used to judge the state of the mouse Default Didn't press
var last = null;// The point at which the current mouse ends drawing
// Definition of drawing function
function pos(event)// A function that determines the coordinates of a point
{
var x1,y1; // Represents the coordinates of the point triggered by the current point
x1=event.clientX;
y1=event.clientY;
return{
x:x1,
y:y1
}
}
// Ready to start drawing
function start(event)
{
mousePress=true;// It means mouse down
}
// Start drawing
function draw(event)
{
if(!mousePress) return;// Indicates that the mouse is not pressed
var xy=pos(event);// Get the point coordinates
if(last != null){ // Indicates that the end point is not empty
pen.beginPath();// Prepare the brush
pen.moveTo(last.x,last.y);// The starting point of the brush Last drawing end point
pen.lineTo(xy.x,xy.y);//lineTo Method Finish line The coordinates of the current point
pen.stroke();// Use this method with real drawings
}
last = xy;// Give the current point to last It means the end of this time
}
// Finish drawing
function finish(event)
{
mousePress=false;// It means mouse down
last=null ;// Set the end point to null
}
canvas.onmousedown=start;
canvas.onmousemove=draw;
canvas.onmouseup=finish;
边栏推荐
- Database design
- Solve the problem that the script tag is written in front of the element node and cannot get the element node
- J. Serval and essay (tarjan finds topological order)
- The third week of summer vacation
- 【MySQL】字符集utf8mb4无法存储表情踩坑记录
- LiteSpeed Web服务器中安装SSL证书
- Spark memory management mechanism new version
- In depth understanding of the underlying framework of wechat applet (II) component system, exprser
- How to solve the problem that the device video cannot be played due to the missing CGO playback callback parameters of easycvr platform?
- Study and use of burpsuite plug-in
猜你喜欢

Thread pool interview

Deliver temperature with science and technology, vivo protects the beauty of biodiversity

深入了解-微信开发者工具
![STM32 installation tutorial and j-link burning driver installation tutorial [the next day]](/img/09/def640c771f1b9effaaec3844d4cd3.png)
STM32 installation tutorial and j-link burning driver installation tutorial [the next day]

145-keep-alive的初步使用

Mysql database authorization learning

Cinq ans de contact avec près d'une centaine de patrons, en tant que chasseur de têtes, j'a i découvert que le secret de la promotion n'est que quatre mots

Try to run this command from the system terminal Make sure that you use the correct

View binding confusion. I have been studying confusion for two days.

Yinshimei Invisalign oral scan referral method (export oral scan data + online consultation)
随机推荐
解决script标签写在元素节点前面无法获取元素节点的问题
MySQL Basics (operators, sorting and paging, multi table queries, functions)
On the possibility and limitation of defi in the metauniverse
NETCORE - how to ensure that icollection or list privatization is not externally modified?
什么叫裸写SQL?express操作mysql用什么中件间或插件好呢?
Excel simple macro
奔走相告,行情与量化页面功能优化!股票量化分析工具QTYX-V2.4.5
Halide::Generator生成器使用说明
浅谈领域驱动设计
毕业设计校园信息发布平台网站源码
[machine learning basics] common operations of Feature Engineering
[bdsec CTF 2022] partial WP
原生组件、小程序与客户端通信原理、video、map、canvas、picker等运行原理
The new red envelope cover platform can build the source code of the independent background of the sub station
架构实战营模块二作业
5年接觸近百比特老板,身為獵頭的我,發現昇職的秘密不過4個字
快速排序注意点
医院网络安全架构
图解数组和链表详细对比,性能测试
Database security and data integrity