鍵盤監聽事件--向左
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>canvas元素示例</title>
<script type="text/javascript"
src="<%=request.getContextPath()%>/js/jquery-1.9.1.js"></script>
<script type="text/javascript">
function draw(id) {
var canvas = document.getElementById(id);
if (canvas == null)
return false;
var context = canvas.getContext('2d');
context.fillStyle = "#EEEEFF"; //設定顏色
context.fillRect(0, 0, 400, 300); //畫正方形
context.fillStyle = "red"; //顏色
context.strokeStyle = "blue"; //邊框顏色
context.lineWidth = 1;//邊框線寬度
context.fillRect(50, 50, 100, 100);
context.strokeRect(50, 50, 100, 100);
var img = new Image();
img.src="<%=request.getContextPath()%>/img/game/askway/a.jpg";
context.drawImage(img,0,0);
setInterval(move,50);
}
var j=1;
var z=1;
function move()
{
$("#part3").css("background-position",-(120 * (j+1)+70*j)+"px "+-(93*z+100*z)+"px");
j++;
if(j==6){
j=0;
z++;
}
if(z==3){
j=0;
z=0;
}
}
var i=50;
function rehua(){
setInterval(redraw,30);
}
function redraw(){
//var context = $("#canvas").getContext("2d");
var context = document.getElementById("canvas").getContext("2d");
context.clearRect(0, 0, 400, 300);
var img = new Image();
img.src="<%=request.getContextPath()%>"
+"/img/game/askway/a.jpg";
context.drawImage(img, i, i);
i++;
}
function renrehua() {
/* alert($("#part5").position().top);
alert($("#part5").position().left);
alert($("#part5").offset().left);
alert($("#part5").offset().top); */
setInterval(zoulu, 100);
}
var k = 1;
var l = 0;
function zoulu() {
//alert($("#part5").css("left")); //auto
//alert($("#part5").offsetLeft); //undifined
$("#part5").css(
"background-position",
-(35 + k * 70 + 60 * k) + "px " + -(15 + l * 100 + l * 35)
+ "px");
k++;
if (k == 4) {
k = 0;
}
$("#part5").css("left", $("#part5").position().left + 5);
//alert($("#part5").position().left);
}
$(document).keydown(function(event){
switch(event.which){
case 37:alert("ddd");break;//左邊
/* case 38:$("#person").css("top",((x-10)+"px"));break;
case 39:$("#person").css("left",((x+10)+"px"));break;
case 40:$("#person").css("top",((x+10)+"px"));break; */
default:break;
}
});
</script>
<style type="text/css">
#part1 {
border: solid 1px blue;
width: 90px;
height: 68px;
background-image: url(<%=request.getContextPath()%>/img/game/askway/a.jpg);
background-repeat: no-repeat;
display: none;
}
#part2 {
border: solid 1px blue;
width: 90px;
height: 68px;
background-image: url(<%=request.getContextPath()%>/img/game/askway/a.jpg);
background-repeat: no-repeat;
background-position: 50% 50%;
display: none;
}
#part3 {
border: solid 1px blue;
width: 70px; /* 960 576 192 192*/
height: 100px;
background-image: url(<%=request.getContextPath()%>/img/game/askway/b.png);
background-repeat: no-repeat;
background-attachment: fixed;
background-position: -120px -0px;
/* background-position:10% 5%; */
}
#part4 {
width: 500px;
height: 400px;
border: blue 1px solid;
/* position:relative; */
}
#part5 {
position: absolute;
width: 70px;
height: 100px;
background-image: url(<%=request.getContextPath()%>/img/game/askway/c.png);
background-repeat: no-repeat;
background-position: -35px -15px;
}
</style>
</head>
<body>
<input type="button" onclick="draw('canvas');" value="移動">
<input type="button" onclick="rehua();" value="持續移動">
<input type="button" onclick="renrehua()" value="人物走動" />
<div id="part1"></div>
<br>
<div id="part2"></div>
<br>
<div id="part3"></div>
<div id="part4">
<div id="part5"></div>
</div>
<h1>canvas元素示例</h1>
<canvas id="canvas" width="400" height="300" />
</body>
</html>
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>canvas元素示例</title>
<script type="text/javascript"
src="<%=request.getContextPath()%>/js/jquery-1.9.1.js"></script>
<script type="text/javascript">
function draw(id) {
var canvas = document.getElementById(id);
if (canvas == null)
return false;
var context = canvas.getContext('2d');
context.fillStyle = "#EEEEFF"; //設定顏色
context.fillRect(0, 0, 400, 300); //畫正方形
context.fillStyle = "red"; //顏色
context.strokeStyle = "blue"; //邊框顏色
context.lineWidth = 1;//邊框線寬度
context.fillRect(50, 50, 100, 100);
context.strokeRect(50, 50, 100, 100);
var img = new Image();
img.src="<%=request.getContextPath()%>/img/game/askway/a.jpg";
context.drawImage(img,0,0);
setInterval(move,50);
}
var j=1;
var z=1;
function move()
{
$("#part3").css("background-position",-(120 * (j+1)+70*j)+"px "+-(93*z+100*z)+"px");
j++;
if(j==6){
j=0;
z++;
}
if(z==3){
j=0;
z=0;
}
}
var i=50;
function rehua(){
setInterval(redraw,30);
}
function redraw(){
//var context = $("#canvas").getContext("2d");
var context = document.getElementById("canvas").getContext("2d");
context.clearRect(0, 0, 400, 300);
var img = new Image();
img.src="<%=request.getContextPath()%>"
+"/img/game/askway/a.jpg";
context.drawImage(img, i, i);
i++;
}
function renrehua() {
/* alert($("#part5").position().top);
alert($("#part5").position().left);
alert($("#part5").offset().left);
alert($("#part5").offset().top); */
setInterval(zoulu, 100);
}
var k = 1;
var l = 0;
function zoulu() {
//alert($("#part5").css("left")); //auto
//alert($("#part5").offsetLeft); //undifined
$("#part5").css(
"background-position",
-(35 + k * 70 + 60 * k) + "px " + -(15 + l * 100 + l * 35)
+ "px");
k++;
if (k == 4) {
k = 0;
}
$("#part5").css("left", $("#part5").position().left + 5);
//alert($("#part5").position().left);
}
$(document).keydown(function(event){
switch(event.which){
case 37:alert("ddd");break;//左邊
/* case 38:$("#person").css("top",((x-10)+"px"));break;
case 39:$("#person").css("left",((x+10)+"px"));break;
case 40:$("#person").css("top",((x+10)+"px"));break; */
default:break;
}
});
</script>
<style type="text/css">
#part1 {
border: solid 1px blue;
width: 90px;
height: 68px;
background-image: url(<%=request.getContextPath()%>/img/game/askway/a.jpg);
background-repeat: no-repeat;
display: none;
}
#part2 {
border: solid 1px blue;
width: 90px;
height: 68px;
background-image: url(<%=request.getContextPath()%>/img/game/askway/a.jpg);
background-repeat: no-repeat;
background-position: 50% 50%;
display: none;
}
#part3 {
border: solid 1px blue;
width: 70px; /* 960 576 192 192*/
height: 100px;
background-image: url(<%=request.getContextPath()%>/img/game/askway/b.png);
background-repeat: no-repeat;
background-attachment: fixed;
background-position: -120px -0px;
/* background-position:10% 5%; */
}
#part4 {
width: 500px;
height: 400px;
border: blue 1px solid;
/* position:relative; */
}
#part5 {
position: absolute;
width: 70px;
height: 100px;
background-image: url(<%=request.getContextPath()%>/img/game/askway/c.png);
background-repeat: no-repeat;
background-position: -35px -15px;
}
</style>
</head>
<body>
<input type="button" onclick="draw('canvas');" value="移動">
<input type="button" onclick="rehua();" value="持續移動">
<input type="button" onclick="renrehua()" value="人物走動" />
<div id="part1"></div>
<br>
<div id="part2"></div>
<br>
<div id="part3"></div>
<div id="part4">
<div id="part5"></div>
</div>
<h1>canvas元素示例</h1>
<canvas id="canvas" width="400" height="300" />
</body>
</html>
相關文章
- 監聽鍵盤事件事件
- javascript監聽鍵盤事件JavaScript事件
- vue的監聽鍵盤事件的快捷方法Vue事件
- Java中的鍵盤監聽事件KeyListenerJava事件
- js監聽鍵盤事件程式碼例項例項JS事件
- python pynput監聽鍵盤Python
- java鍵盤監聽之視窗監聽的實現Java
- java全域性滑鼠鍵盤監聽Java
- Android 監聽鍵盤彈出收起Android
- 事件監聽事件
- Android 監聽鍵盤狀態變化,並獲取鍵盤高度Android
- Flutter事件監聽Flutter事件
- jQuery事件監聽jQuery事件
- 監聽滑鼠事件事件
- JavaScript 事件監聽JavaScript事件
- js 監聽事件JS事件
- 事件和事件監聽器事件
- h5監聽手機鍵盤彈起H5
- Android 監聽鍵盤彈起及隱藏Android
- Android監聽軟鍵盤收起與彈出Android
- 鍵盤事件事件
- vue之監聽事件Vue事件
- 初識事件監聽事件
- JS事件監聽器JS事件
- WINDOWS鍵盤事件監控原理及應用 (轉)Windows事件
- flutter中監聽鍵盤Flutter
- 滑鼠、鍵盤事件事件
- swift 訊息監聽和鍵值監聽(kvo)Swift
- deleted事件監聽報錯delete事件
- 如何移除事件監聽器事件
- Laravel 中的事件監聽Laravel事件
- passive 的事件監聽器事件
- java 自定義監聽事件Java事件
- springboot事件監聽Spring Boot事件
- android設定軟鍵盤搜尋鍵以及監聽搜尋鍵點選時發生兩次事件的問題解決Android事件
- java監聽組合鍵Java
- 監聽所有模型的 saved 事件模型事件
- 如何監聽SAP CRM BOR事件事件