canvas 像素级碰撞
demo地址http://06wjin.sinaapp.com/html5/hitTest/ 用鼠标拖动
1首先先判断两个图片的矩形区域有无碰撞
判断两个矩形的中心距离即可
2如果矩形区域有碰撞则检测上图红色矩形区域的像素,这里有两种方法
a 直接判断,循环检测两位图在红色矩形内是否有一点像素的alpha点都不为0,如果有则说明碰撞
- context.drawImage(img1, x1, y1);//画第一张位图
- var data1 = context.getImageData(minx, miny, maxx - minx, maxy - miny).data;//获取第一张位图红色矩形内像素
- context.clearRect(x1, y1,img1.width,img1.height);//清除第一张位图
- context.drawImage(img2, x2, y2);//画第二张位图
- var data2 = context.getImageData(minx, miny, maxx - minx, maxy - miny).data;//获取第二章位图红色矩形内像素
- for(var i = 3; i
- {
- if(data1 > 0 && data2 > 0) return true;//循环判断alpha值,这里可以设置透明阙值,比如把0改为0.2,意味着透明度为0.2时就认为不碰撞了
- }
- return false;
b将绘图模式改为xor(xor是指相交部分透明,具体见站长上篇教程),也可以判断
- context.drawImage(img1, x1,y1);//画第一张位图
- context.globalCompositeOperation = 'xor';//改绘图模式为xor
- context.drawImage(img2, x2,y2);//画第二张位图
- var data = context.getImageData(minx, miny, maxx - minx, maxy - miny).data;//获取位图红色矩形内像素
- context.globalCompositeOperation = 'source-over';//把绘图模式改回去
- for(var i = 3; i
- {
- if(data == 0 ) return true;//若有透明像素,则碰撞
- }
- return false;
下面是一个2d精灵类,碰撞直接用sprite1.hitTest(sprite2)就好
- function Sprite(x, y, img, width, height)
- {
- this.x = x;
- this.y = y;
- this.img = document.getElementById(img);
- this.width = width;
- this.height = height;
- this.halfWidth = this.width/2;
- this.halfHeight = this.height/2;
- this.angle = 0;角度
- this.scaleX = 1;//水平缩放
- this.scaleY = 1;//竖直缩放
- this.alpha = 1;//透明度
- this.isDrug = false;//是否拖到
- }
- Sprite.prototype.draw = function()
- {
- context.save();
- context.translate(this.x + this.halfWidth, this.y + this.halfHeight);
- context.globalAlpha = this.alpha;//修改透明度
- context.rotate(this.angle);//旋转角度
- context.scale(this.scaleX, this.scaleY);//缩放
- context.drawImage(this.img, -this.halfWidth, -this.halfHeight);
- context.restore();
- }
- Sprite.prototype.hitTest = function(sprite)
- {
- var minx = this.x > sprite.x ? this.x :sprite.x;
- var maxx = this.x + this.width
- var miny = this.y > sprite.y ? this.y : sprite.y;
- var maxy = this.y + this.width
- if (minx >= maxx || miny >= maxy) {return false;}
- var canvas = document.createElement('canvas');
- canvas.setAttribute('width', 550);
- canvas.setAttribute('height', 400);
- var context = canvas.getContext('2d');
- /*第一种方法*/
- context.drawImage(this.img, this.x, this.y);
- var data1 = context.getImageData(minx, miny, maxx - minx, maxy - miny).data;
- context.clearRect(0, 0, 550, 400);
- context.drawImage(sprite.img, sprite.x, sprite.y);
- var data2 = context.getImageData(minx, miny, maxx - minx, maxy - miny).data;
- for(var i = 3; i
- {
- if(data1 > 0 && data2 > 0) return true;
- }
- return false;
- /*第二种方法
- context.drawImage(this.img, this.x, this.y);
- context.globalCompositeOperation = 'xor';
- context.drawImage(sprite.img, sprite.x, sprite.y);
- var data = context.getImageData(minx, miny, maxx - minx, maxy - miny).data;
- context.globalCompositeOperation = 'source-over';
- for(var i = 3; i
- {
- if(data == 0 ) return true;
- }
- return false;*/
- }

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



How uniapp can achieve rapid conversion between mini programs and H5 requires specific code examples. In recent years, with the development of the mobile Internet and the popularity of smartphones, mini programs and H5 have become indispensable application forms. As a cross-platform development framework, uniapp can quickly realize the conversion between small programs and H5 based on a set of codes, greatly improving development efficiency. This article will introduce how uniapp can achieve rapid conversion between mini programs and H5, and give specific code examples. 1. Introduction to uniapp unia

Comparing the two versions of win1121h2 and 22h2, the latter 22h2 is more stable, and 22h2 has more functions. Compared with the previous 21h2, many functions have been improved. Let’s take a look. Which one is more stable, win1121h2 or 22h2: Answer: 22h2 is more stable. Comparing win1121h2 and 22h2, 22h2 is more stable. 22h2 adds a lot of features, and the problems of 21h2 have also been improved in 22h2. 22h2 update feature: Applications folder in start menu. Adjustable fixed area in the Start menu. Drag and drop on the taskbar. Focus Assist is integrated with the Notification Center. New "Spotlight" wallpaper feature. new

Applicable scenarios: 1. The project scale is not large. 2. The number of users is not very large, and the concurrency requirements are not strong. 3. There is no dedicated operation and maintenance force. 4. Exquisite team size. For some regular projects, or units where the division of corporate responsibilities is not very clear. explain. Often a system goes from requirements to design, development, testing to final launch, operation and maintenance. Often 80% of the tasks are completed by the development team. Therefore, in addition to implementing the system's functions, developers must also provide customers with consultation and answer questions and solve production problems. Just imagine, after an application is launched, there are no monitoring measures. Just like driving a car without any dashboard, no one feels safe on the road like this. How to balance simplicity and efficiency is something worth thinking about. 1. Springb

Windows 101909 is currently considered one of the most stable and reliable versions. However, unfortunately, service support for this version has ended recently. 21H2 is a relatively stable version. In fact, from the actual situation, both are very good choices. Which one is better, win101909 or 21h2? Answer: 1909 is more stable, and 21h2 will be more secure. In the current environment, 1909 is still generally regarded as one of the most stable and reliable versions. However, the Win101909 version has officially stopped serving on May 11, 2021. WindowsServer21h2 is committed to providing more professional IT function support to the majority of users. 1. After actual testing by many users,

The 23h2 version and the 22h2 version in the Windows 11 system will be released in 2023 and 2022 respectively. Generally speaking, system updates are getting better and better. The editor also believes that the 23h2 version is better than the 22h2 version. Which one is better, win1123h2 or 22h2? Answer: win1123h2 is better. According to reports, win1123h2 is a cumulative version update from 22h2 to the next version, and they are all the same platform. This means that there are no compatibility issues between the two versions. It is recommended that you update them in time. The win1123h2 version brings us many practical features, such as the never-merge mode for taskbar window applications. There are more

Everyone wants to update the 23H2 version of Win11 recently, but a small number of users have not received the update push message yet. It may be that a process in the background update progress is stuck, and it will be fine after a while. What to do if the Win11 update fails to get 23H2. Method 1: Wait patiently. If the user checks the update status of the computer and finds that it is stuck, we can wait for a while and the system will continue to update. Method 2: Clear the updated cache. If the user has updated the system before and has not cleared the more detailed cache, it will affect the normal update of 23h2. You can manually clear it. Method three: Use image installation. It is recommended that you go to Microsoft's official website to download the image file of win1123h2, and then update the file.

You need the permissions provided by admin to make changes to this file. Solution: 1. After selecting the administrator account on the login interface and entering the password, you can modify the file smoothly; 2. You can right-click the file and select "As Administrator" Solution: 3. Modify file permissions, right-click the file, select "Properties", click the "Security" tab, then click the "Edit" button, select your username, and then check the "Full Control" option ; 4. Use the command prompt to solve the problem; 5. Set UA permissions.

How to use Flask-Admin to implement the backend management interface Background introduction: With the development of websites and applications, the backend management interface is becoming more and more important. During the development process, we often need a convenient and fast backend management interface to manage data, users and other important information. Flask-Admin is a powerful and easy-to-use Flask extension that can help us quickly implement the background management interface. Flask-Admin is an open source project based on Flask and SQLAlchemy
