用JavaScript實(shí)現(xiàn)使用鼠標(biāo)畫線的示例代碼
更新時(shí)間:2014年08月19日 17:07:44 投稿:whsnow
用JavaScript實(shí)現(xiàn)用鼠標(biāo)畫線,具體步驟是首先是畫點(diǎn),在根據(jù)兩點(diǎn)坐標(biāo)畫直線,最后是獲取鼠標(biāo)位置,需要的朋友可以參考下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled 1</title>
<style type="text/css">
.style1 {
font-size: x-small;
}
</style>
<script type="text/javascript">
/**
畫點(diǎn)
*/
function makedot(x, y){
pointDiv = "<div style='height:1px;position:absolute;left:" + x +
"px;top:" + y + "px;width:1px;background:#f00;overflow:hidden'></div>";
return pointDiv;
}
/**
根據(jù)兩點(diǎn)坐標(biāo)畫直線。
*/
function line(x1,y1,x2,y2){
var slope; //斜率
var direction;//坐標(biāo)運(yùn)動(dòng)方向
var tx = x2 - x1;
var ty = y2 - y1;
if(tx == 0 && ty == 0)return;
var points = "";
var axis;//坐標(biāo)軸上的坐標(biāo)
if(Math.abs(tx) >= Math.abs(ty)){//在x軸上移動(dòng)
direction = tx > 0 ? 1 : -1;
tx = Math.abs(tx);
slope = ty / tx;
axis = x1;
for(i = 0; i < tx; i ++){
points += makedot(axis, y1 + i * slope);
axis += direction;
}
}else{//在y軸上移動(dòng)
direction = ty > 0 ? 1 : -1;
ty = Math.abs(ty);
slope = tx / ty;
axis = y1;
for(i = 0; i < ty; i ++){
points += makedot(x1 + i * slope, axis);
axis += direction;
}
}
var container = document.getElementById("container");
container.innerHTML += points;
}
var oldPoint = null;
//獲取鼠標(biāo)位置
function mousePosition(ev){
ev = ev || window.event;
if(ev.pageX || ev.pageY){
return {x:ev.pageX, y:ev.pageY};
}
var doc = document.documentElement, body = document.body;
var pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0);
var pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0);
return {x:pageX, y:pageY};
}
function recordPoint(ev){
var point = mousePosition(ev);
if(oldPoint != null){
line(oldPoint.x, oldPoint.y, point.x, point.y);
}
oldPoint = point;
}
</script>
</head>
<body>
<div id="container" style="width: 1000px; height: 600px; border:1px #bfbfbf solid;" onclick="recordPoint(event);">
</div>
<script type="text/javascript">
//line(19,19,22,300);
</script>
</body>
</html>
您可能感興趣的文章:
- JavaScript實(shí)現(xiàn)鼠標(biāo)滑過處生成氣泡的方法
- javascript實(shí)現(xiàn)圖片跟隨鼠標(biāo)移動(dòng)效果的方法
- JavaScript實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊后層展開效果的方法
- 最精簡(jiǎn)的JavaScript實(shí)現(xiàn)鼠標(biāo)拖動(dòng)效果的方法
- javascript實(shí)時(shí)獲取鼠標(biāo)坐標(biāo)值并顯示的方法
- javascript實(shí)現(xiàn)鼠標(biāo)拖動(dòng)改變層大小的方法
- JavaScript實(shí)現(xiàn)鼠標(biāo)滑過圖片變換效果的方法
- javascript實(shí)現(xiàn)簡(jiǎn)單的鼠標(biāo)拖動(dòng)效果實(shí)例
- javascript獲取當(dāng)前鼠標(biāo)坐標(biāo)的方法
- JavaScript中獲取鼠標(biāo)位置相關(guān)屬性總結(jié)
- JavaScript檢測(cè)鼠標(biāo)移動(dòng)方向的方法
相關(guān)文章
bootstrap 路徑導(dǎo)航 分頁 進(jìn)度條的實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了bootstrap 路徑導(dǎo)航 分頁 進(jìn)度條的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
javascript支持區(qū)號(hào)輸入的省市二級(jí)聯(lián)動(dòng)下拉菜單
javascript支持區(qū)號(hào)輸入的省市二級(jí)聯(lián)動(dòng)下拉菜單...2007-05-05
基于JavaScript實(shí)現(xiàn)留言板功能
這篇文章主要為大家詳細(xì)介紹了基于JavaScript實(shí)現(xiàn)留言板功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
JavaScript和HTML DOM的區(qū)別與聯(lián)系及Javascript和DOM的關(guān)系
這篇文章主要介紹了JavaScript和HTML DOM的區(qū)別與聯(lián)系及Javascript和DOM的關(guān)系的相關(guān)資料,需要的朋友可以參考下2015-11-11
你應(yīng)該了解的JavaScript Array.map()五種用途小結(jié)
大家都知道m(xù)ap() 方法返回一個(gè)新數(shù)組,數(shù)組中的元素為原始數(shù)組元素調(diào)用函數(shù)處理后的值。下面這篇文章主要給大家介紹了關(guān)于JavaScript Array.map()的五種用途,需要的朋友可以參考下2018-11-11
js 復(fù)制功能 支持 for IE/FireFox/mozilla/ns
js 復(fù)制功能 支持 for IE/FireFox/mozilla/ns...2007-11-11

