簡單前端——圓與圓碰撞檢測
只是沒什麼做搞個來玩玩,順便練練手
滑鼠按著紅球,就可以移動紅球,如果以黃球發生碰撞,紅球就變為綠球,否則顏色不變。
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title></title>
- <style>
- #a , #b {
- width: 100px;
- height: 100px;
- position: absolute;
- border-radius: 50%;
- }
- #a{
- left: 0;
- top: 0;
- background-color: red;
- z-index: 1;
- }
- #b {
- left: 0;
- top: 0;
- right: 0;
- bottom: 0;
- margin: auto;
- background-color: yellow;
- }
- </style>
- </head>
- <body>
- <div id="a"></div>
- <div id="b"></div>
- <script>
- var a = document.getElementById('a');
- var b = document.getElementById('b');
- a.onmousedown = function (ev) {
- aX = ev.clientX - a.offsetLeft;
- aY = ev.clientY - a.offsetTop;
- document.onmousemove = function (ev) {
- var t1 = a.offsetTop;
- var l1 = a.offsetLeft;
- var t2 = b.offsetTop;
- var l2 = b.offsetLeft;
- if (Math.sqrt(Math.pow(t1 - t2, 2) + Math.pow(l1 - l2, 2)) <= 100) {
- // 如果兩圓的圓心距小於或等於兩圓半徑和則認為發生碰撞
- a.style.background = 'green';
- }else{
- a.style.background = 'red';
- }
- a.style.left = ev.clientX - aX +'px';
- a.style.top = ev.clientY - aY +'px';
- }
- document.onmouseup = function () {
- document.onmousemove = null;
- document.onmouseup = null;
- }
- return false;
- }
- </script>
- </body>
- </html>
轉載來自:http://blog.csdn.net/zhuxiongyin/article/details/71499843
相關文章
- javascript圓形區域碰撞檢測程式碼JavaScript
- SDL程式設計入門(29)圓形碰撞檢測程式設計
- 霍夫檢測圓:霍夫梯度法梯度
- 簡單的CSS圓形縮放動畫CSS動畫
- canvas的簡單圓形進度條Canvas
- 簡單介紹Pygame 精準檢測影像碰撞的問題GAM
- 堅果Pro簡單上手評測 圓滑之中的銳麗異類
- 一個簡單的CSS圓形縮放動畫CSS動畫
- canvas簡單動畫案例(圓圈閃爍漸變動畫)Canvas動畫
- CSS3簡單圓角立體按鈕效果CSSS3
- 霍夫變換圓檢測原理及 OpenCV API 應用OpenCVAPI
- 【OpenCV】影像變換(四-2)霍夫變換圓檢測OpenCV
- 最簡單的Qt程式:根據使用者所輸入圓半徑計算圓面積QT
- iOS高效簡易新增圓角iOS
- Flutter 圓形/圓角頭像Flutter
- 圓角select下拉選單
- 筆記-圓角四種方法的對比以及效能檢測筆記
- 計算機視覺 OpenCV Android | 基本特徵檢測之 霍夫圓檢計算機視覺OpenCVAndroid特徵
- WPF 2D 碰撞檢測
- javascript矩形碰撞檢測程式碼JavaScript
- 自定義檢視---圓角柱狀圖(一)
- Gif(1)-載入檢視-交替圓效果
- canvas繪製圓形圖案程式碼示例簡單介紹Canvas
- css實現圓形、橢圓和半圓效果程式碼例項CSS
- 第三章:勾股陣列與單位圓陣列
- Android 圓角、圓形 ImageView 實現AndroidView
- 圓滑細膩!那些使用圓形導航選單的漂亮網頁設計網頁
- 學習 PixiJS — 碰撞檢測JS
- Core Graphic 指南:圓弧與路徑
- CSS3邊框與圓角CSSS3
- .net core(c#)擬合圓測試C#
- 圓形、扇形選單,支援移動、桌面
- WEBGL橢圓Web
- [原創]SpriteKit+Swift學習筆記(七)-簡述碰撞檢測Swift筆記
- 簡單票據檢測方法
- 華為車輛碰撞檢測專利公佈,可檢測車輛能否碰撞潛在障礙物
- WPS表格研究:圓和圓的位置關係
- python如何檢測pygame中的碰撞PythonGAM