HTML5-canvas 学习
~~canvas矩形框,可创建、消除、重置
最近需要完成一个小demo:初始化一个canvas,通过鼠标的down、move、up事件来画矩形框。
查了很多资料,发现都没有我想要的。只好自己尝试写一个,下面是一些参考的网址:
http://jsfiddle.net/a7mad24/aPLq5/
废话不多直接上demo:
首先是html页面:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
demo 1
</title>
<script src="jquery-1.10.2.min.js">
</script>
<script type="text/javascript" src="shapes.js">
</script>
</head>
<body onload="init()">
<div id="body">
<div>
<button id="reset" class="reset">
<span>
Reset
</span>
</button>
<button id="create" class="create">
<span>
Create
</span>
</button>
<button id="state" class="state">
<span id="txt">
Start
</span>
</button>
</div>
<div id="container">
<canvas id="canvas0" width="200" height="150" style="border: 1px solid black;background-color: rgb(94, 255, 0);">
</canvas>
</div>
</div>
</body>
<script>
var CanvasAry;
var edit = false;
function init() {
CanvasAry = new Array();
var canvas = document.getElementById('canvas0');
var s = new CanvasState(canvas);
s.addShape(new Shape(0, 0, 60, 30, 'rgba(96, 96, 96, .5)', true));
s.addShape(new Shape(30, 30, 60, 30, 'rgba(96, 96, 96, .5)', true));
CanvasAry[0] = s;
start();
end();
}
$(document).on('click', ".reset",
function(event) {
edit = false;
$("canvas").remove();
$("#txt").html("Start");
var container = document.getElementById('container');
var canvas = document.createElement('canvas');
canvas.id = 'canvas0';
canvas.width = 200;
canvas.height = 150;
canvas.style.border = '1px solid black';
canvas.style.backgroundColor = 'rgb(94, 255, 0)';
container.appendChild(canvas);
init();
});
$(document).on('click', ".state",
function(event) {
var txt = $("#txt")[0].innerHTML;
if (txt == "Start") {
start();
} else {
end();
}
});
$(document).on('click', ".create",
function(event) {
var container = document.getElementById('container');
var l = CanvasAry.length;
var newId = 'canvas' + (l + 1);
var canvas = document.createElement('canvas');
canvas.id = newId;
canvas.width = 200;
canvas.height = 150;
canvas.style.border = '1px solid black';
canvas.style.backgroundColor = 'rgb(94, 255, 0)';
container.appendChild(canvas);
var newS = new CanvasState(canvas);
CanvasAry[l] = newS;
var txt = $("#txt")[0].innerHTML;
if (txt == "End") {
newS.start();
}
});
function start() {
var l = CanvasAry.length;
edit = true;
for (var i = 0; i < l; i++) {
CanvasAry[i].start();
redraw(CanvasAry[i]);
}
$("#txt").html("End");
}
function end() {
var l = CanvasAry.length;
edit = false;
for (var i = 0; i < l; i++) {
redraw(CanvasAry[i]);
CanvasAry[i].end();
}
$("#txt").html("Start");
}
function redraw(myState) {
var ctx = myState.ctx;
var shapes = myState.shapes;
myState.clear();
var l = shapes.length;
for (var i = 0; i < l; i++) {
var shape = shapes[i];
shapes[i].draw(ctx);
}
}
</script>
</html>
然后就是主要的js:shape.js
function Shape(x, y, w, h, fill, finished) {
this.x = x || 0;
this.y = y || 0;
this.w = w || 1;
this.h = h || 1;
this.fill = fill || 'rgba(96, 96, 96, .5)';
this.finished = finished;
}
Shape.prototype.draw = function(ctx) {
ctx.fillStyle = this.fill;
ctx.fillRect(this.x, this.y, this.w, this.h);
if(edit){
if (this.finished) {
var img=new Image();
img.src="x_20.png";
var ix=this.x + this.w - 20;
var iy=this.y;
if(this.w<0){
ix=this.x-20;
}
if(this.h<0){
iy=this.y+this.h;
}
ctx.drawImage(img, ix, iy, 20, 20);
}
}
}
Shape.prototype.contains = function(mx, my) {
return (this.x <= mx) && (this.x + this.w >= mx) && (this.y <= my) && (this.y + this.h >= my);
}
Shape.prototype.needClose = function(mx, my) {
var minx=this.x + this.w - 20;
var miny=this.y;
if(this.w<0){
minx=this.x-20;
}
if(this.h<0){
miny=this.y+this.h;
}
return (minx <= mx) && (minx+20 >= mx) && (miny <= my) && (miny + 20 >= my);
}
function CanvasState(canvas) {
this.canvas = canvas;
this.width = canvas.width;
this.height = canvas.height;
this.ctx = canvas.getContext('2d');
var stylePaddingLeft, stylePaddingTop, styleBorderLeft, styleBorderTop;
if (document.defaultView && document.defaultView.getComputedStyle) {
this.stylePaddingLeft = parseInt(document.defaultView.getComputedStyle(canvas, null)['paddingLeft'], 10) || 0;
this.stylePaddingTop = parseInt(document.defaultView.getComputedStyle(canvas, null)['paddingTop'], 10) || 0;
this.styleBorderLeft = parseInt(document.defaultView.getComputedStyle(canvas, null)['borderLeftWidth'], 10) || 0;
this.styleBorderTop = parseInt(document.defaultView.getComputedStyle(canvas, null)['borderTopWidth'], 10) || 0;
}
var html = document.body.parentNode;
this.htmlTop = html.offsetTop;
this.htmlLeft = html.offsetLeft;
this.valid = false;
this.shapes = [];
this.dragging = false;
this.isDown = false;
this.selection = null;
this.dragoffx = 0;
this.dragoffy = 0;
this.rect;
var myState = this;
var funSelectStart=function(e) {
e.preventDefault();
return false;
};
var funMouseDown=function(e) {
var mouse = myState.getMouse(e);
var mx = mouse.x;
var my = mouse.y;
if (!myState.isDown) {
var shapes = myState.shapes;
var l = shapes.length;
for (var i = l - 1; i >= 0; i--) {
if (shapes[i].needClose(mx, my)) {
var mySel = shapes[i];
myState.deleteShape(mySel);
myState.draw();
return;
}
if (shapes[i].contains(mx, my)) {
var mySel = shapes[i];
myState.dragoffx = mx - mySel.x;
myState.dragoffy = my - mySel.y;
myState.dragging = true;
myState.selection = mySel;
myState.valid = false;
return;
}
}
if (myState.selection) {
myState.selection = null;
myState.valid = false;
}
var newRect=new Shape(mx, my, 0, 0, 'rgba(96, 96, 96, .5)', false);
myState.addShape(newRect);
myState.setRect(newRect);
myState.setIsDown(true);
}
};
var funMouseMove= function(e) {
var mouse = myState.getMouse(e);
if (myState.dragging) {
myState.selection.x = mouse.x - myState.dragoffx;
myState.selection.y = mouse.y - myState.dragoffy;
myState.valid = false;
}
if (myState.isDown) {
var shapes = myState.shapes;
var rect=myState.rect;
var rex = rect.x;
var rey = rect.y;
var rew = mouse.x - rex
var reh = mouse.y - rey;
myState.deleteShape(rect);
var newRect=new Shape(rex, rey, rew, reh, 'rgba(96, 96, 96, .5)', false);
myState.addShape(newRect);
myState.setRect(newRect);
}
};
var funMouseUp=function(e) {
myState.dragging = false;
if (myState.isDown) {
var shapes = myState.shapes;
var rect=myState.rect;
var rex = rect.x;
var rey = rect.y;
var rew = rect.w;
var reh = rect.h;
myState.deleteShape(myState.rect);
var ifBadW=false;
var ifBadH=false;
if(rew>=0){
if(rew <= 2){
ifBadW=true;
}
if(rew<40){
rew=40;
}
}else{
if(rew >= -2){
ifBadW=true;
}
if(rew>-40){
rew=-40;
}
}
if(reh>=0){
if(reh <= 2){
ifBadH=true;
}
if(reh<20){
reh=20;
}
}else{
if(reh >= -2){
ifBadH=true;
}
if(reh>-20){
reh=-20;
}
}
if (!(ifBadW&&ifBadH)){
myState.addShape(new Shape(rex, rey, rew, reh, 'rgba(96, 96, 96, .5)', true));
}
}
myState.setIsDown(false);
};
this.start=function(){
canvas.addEventListener('selectstart',funSelectStart,false);
canvas.addEventListener('mousedown',funMouseDown, true);
canvas.addEventListener('mousemove',funMouseMove, true);
canvas.addEventListener('mouseup',funMouseUp,true);
};
this.end=function(){
canvas.removeEventListener('selectstart',funSelectStart,false);
canvas.removeEventListener('mousedown',funMouseDown, true);
canvas.removeEventListener('mousemove',funMouseMove, true);
canvas.removeEventListener('mouseup',funMouseUp,true);
};
setInterval(function() {
myState.draw();
},
myState.interval);
}
CanvasState.prototype.setIsDown = function(isDown) {
this.isDown = isDown;
}
CanvasState.prototype.setRect = function(rect) {
this.rect = rect;
}
CanvasState.prototype.addShape = function(shape) {
this.shapes.push(shape);
this.valid = false;
}
CanvasState.prototype.deleteShape = function(shape) {
this.shapes.splice($.inArray(shape, this.shapes), 1);
this.valid = false;
}
CanvasState.prototype.clear = function() {
this.ctx.clearRect(0, 0, this.width, this.height);
}
CanvasState.prototype.draw = function() {
if (!this.valid) {
var ctx = this.ctx;
var shapes = this.shapes;
this.clear();
var l = shapes.length;
for (var i = 0; i < l; i++) {
var shape = shapes[i];
if (shape.x > this.width || shape.y > this.height || shape.x + shape.w < 0 || shape.y + shape.h < 0) continue;
shapes[i].draw(ctx);
}
this.valid = true;
}
}
CanvasState.prototype.getMouse = function(e) {
var element = this.canvas,
offsetX = 0,
offsetY = 0,
mx, my;
if (element.offsetParent !== undefined) {
do {
offsetX += element.offsetLeft;
offsetY += element.offsetTop;
} while (( element = element . offsetParent ));
}
offsetX += this.stylePaddingLeft + this.styleBorderLeft + this.htmlLeft;
offsetY += this.stylePaddingTop + this.styleBorderTop + this.htmlTop;
mx = e.pageX - offsetX;
my = e.pageY - offsetY;
return {
x: mx,
y: my
};
}
需要引入jquery-1.10.2.min.js
图片:
效果如下:
1. 初始化 不可修改。
2. 点击start 可通过叉来消除,鼠标按下、移动、松开来创建,同时矩形可通过鼠标来移动。
3. 点击end 去除叉,同时去除鼠标事件。
4. 点击reset 返回初始页面。
5. 点击create
可创建多个canvas,同时可以通过按钮来控制事件。
初级版本仅供参考,后续有升级版本,添加功能按比例缩放。
~~canvas矩形框,可创建、消除、重置
最近需要完成一个小demo:初始化一个canvas,通过鼠标的down、move、up事件来画矩形框。
查了很多资料,发现都没有我想要的。只好自己尝试写一个,下面是一些参考的网址:
http://jsfiddle.net/a7mad24/aPLq5/
废话不多直接上demo:
首先是html页面:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
demo 1
</title>
<script src="jquery-1.10.2.min.js">
</script>
<script type="text/javascript" src="shapes.js">
</script>
</head>
<body onload="init()">
<div id="body">
<div>
<button id="reset" class="reset">
<span>
Reset
</span>
</button>
<button id="create" class="create">
<span>
Create
</span>
</button>
<button id="state" class="state">
<span id="txt">
Start
</span>
</button>
</div>
<div id="container">
<canvas id="canvas0" width="200" height="150" style="border: 1px solid black;background-color: rgb(94, 255, 0);">
</canvas>
</div>
</div>
</body>
<script>
var CanvasAry;
var edit = false;
function init() {
CanvasAry = new Array();
var canvas = document.getElementById('canvas0');
var s = new CanvasState(canvas);
s.addShape(new Shape(0, 0, 60, 30, 'rgba(96, 96, 96, .5)', true));
s.addShape(new Shape(30, 30, 60, 30, 'rgba(96, 96, 96, .5)', true));
CanvasAry[0] = s;
start();
end();
}
$(document).on('click', ".reset",
function(event) {
edit = false;
$("canvas").remove();
$("#txt").html("Start");
var container = document.getElementById('container');
var canvas = document.createElement('canvas');
canvas.id = 'canvas0';
canvas.width = 200;
canvas.height = 150;
canvas.style.border = '1px solid black';
canvas.style.backgroundColor = 'rgb(94, 255, 0)';
container.appendChild(canvas);
init();
});
$(document).on('click', ".state",
function(event) {
var txt = $("#txt")[0].innerHTML;
if (txt == "Start") {
start();
} else {
end();
}
});
$(document).on('click', ".create",
function(event) {
var container = document.getElementById('container');
var l = CanvasAry.length;
var newId = 'canvas' + (l + 1);
var canvas = document.createElement('canvas');
canvas.id = newId;
canvas.width = 200;
canvas.height = 150;
canvas.style.border = '1px solid black';
canvas.style.backgroundColor = 'rgb(94, 255, 0)';
container.appendChild(canvas);
var newS = new CanvasState(canvas);
CanvasAry[l] = newS;
var txt = $("#txt")[0].innerHTML;
if (txt == "End") {
newS.start();
}
});
function start() {
var l = CanvasAry.length;
edit = true;
for (var i = 0; i < l; i++) {
CanvasAry[i].start();
redraw(CanvasAry[i]);
}
$("#txt").html("End");
}
function end() {
var l = CanvasAry.length;
edit = false;
for (var i = 0; i < l; i++) {
redraw(CanvasAry[i]);
CanvasAry[i].end();
}
$("#txt").html("Start");
}
function redraw(myState) {
var ctx = myState.ctx;
var shapes = myState.shapes;
myState.clear();
var l = shapes.length;
for (var i = 0; i < l; i++) {
var shape = shapes[i];
shapes[i].draw(ctx);
}
}
</script>
</html>
然后就是主要的js:shape.js
function Shape(x, y, w, h, fill, finished) {
this.x = x || 0;
this.y = y || 0;
this.w = w || 1;
this.h = h || 1;
this.fill = fill || 'rgba(96, 96, 96, .5)';
this.finished = finished;
}
Shape.prototype.draw = function(ctx) {
ctx.fillStyle = this.fill;
ctx.fillRect(this.x, this.y, this.w, this.h);
if(edit){
if (this.finished) {
var img=new Image();
img.src="x_20.png";
var ix=this.x + this.w - 20;
var iy=this.y;
if(this.w<0){
ix=this.x-20;
}
if(this.h<0){
iy=this.y+this.h;
}
ctx.drawImage(img, ix, iy, 20, 20);
}
}
}
Shape.prototype.contains = function(mx, my) {
return (this.x <= mx) && (this.x + this.w >= mx) && (this.y <= my) && (this.y + this.h >= my);
}
Shape.prototype.needClose = function(mx, my) {
var minx=this.x + this.w - 20;
var miny=this.y;
if(this.w<0){
minx=this.x-20;
}
if(this.h<0){
miny=this.y+this.h;
}
return (minx <= mx) && (minx+20 >= mx) && (miny <= my) && (miny + 20 >= my);
}
function CanvasState(canvas) {
this.canvas = canvas;
this.width = canvas.width;
this.height = canvas.height;
this.ctx = canvas.getContext('2d');
var stylePaddingLeft, stylePaddingTop, styleBorderLeft, styleBorderTop;
if (document.defaultView && document.defaultView.getComputedStyle) {
this.stylePaddingLeft = parseInt(document.defaultView.getComputedStyle(canvas, null)['paddingLeft'], 10) || 0;
this.stylePaddingTop = parseInt(document.defaultView.getComputedStyle(canvas, null)['paddingTop'], 10) || 0;
this.styleBorderLeft = parseInt(document.defaultView.getComputedStyle(canvas, null)['borderLeftWidth'], 10) || 0;
this.styleBorderTop = parseInt(document.defaultView.getComputedStyle(canvas, null)['borderTopWidth'], 10) || 0;
}
var html = document.body.parentNode;
this.htmlTop = html.offsetTop;
this.htmlLeft = html.offsetLeft;
this.valid = false;
this.shapes = [];
this.dragging = false;
this.isDown = false;
this.selection = null;
this.dragoffx = 0;
this.dragoffy = 0;
this.rect;
var myState = this;
var funSelectStart=function(e) {
e.preventDefault();
return false;
};
var funMouseDown=function(e) {
var mouse = myState.getMouse(e);
var mx = mouse.x;
var my = mouse.y;
if (!myState.isDown) {
var shapes = myState.shapes;
var l = shapes.length;
for (var i = l - 1; i >= 0; i--) {
if (shapes[i].needClose(mx, my)) {
var mySel = shapes[i];
myState.deleteShape(mySel);
myState.draw();
return;
}
if (shapes[i].contains(mx, my)) {
var mySel = shapes[i];
myState.dragoffx = mx - mySel.x;
myState.dragoffy = my - mySel.y;
myState.dragging = true;
myState.selection = mySel;
myState.valid = false;
return;
}
}
if (myState.selection) {
myState.selection = null;
myState.valid = false;
}
var newRect=new Shape(mx, my, 0, 0, 'rgba(96, 96, 96, .5)', false);
myState.addShape(newRect);
myState.setRect(newRect);
myState.setIsDown(true);
}
};
var funMouseMove= function(e) {
var mouse = myState.getMouse(e);
if (myState.dragging) {
myState.selection.x = mouse.x - myState.dragoffx;
myState.selection.y = mouse.y - myState.dragoffy;
myState.valid = false;
}
if (myState.isDown) {
var shapes = myState.shapes;
var rect=myState.rect;
var rex = rect.x;
var rey = rect.y;
var rew = mouse.x - rex
var reh = mouse.y - rey;
myState.deleteShape(rect);
var newRect=new Shape(rex, rey, rew, reh, 'rgba(96, 96, 96, .5)', false);
myState.addShape(newRect);
myState.setRect(newRect);
}
};
var funMouseUp=function(e) {
myState.dragging = false;
if (myState.isDown) {
var shapes = myState.shapes;
var rect=myState.rect;
var rex = rect.x;
var rey = rect.y;
var rew = rect.w;
var reh = rect.h;
myState.deleteShape(myState.rect);
var ifBadW=false;
var ifBadH=false;
if(rew>=0){
if(rew <= 2){
ifBadW=true;
}
if(rew<40){
rew=40;
}
}else{
if(rew >= -2){
ifBadW=true;
}
if(rew>-40){
rew=-40;
}
}
if(reh>=0){
if(reh <= 2){
ifBadH=true;
}
if(reh<20){
reh=20;
}
}else{
if(reh >= -2){
ifBadH=true;
}
if(reh>-20){
reh=-20;
}
}
if (!(ifBadW&&ifBadH)){
myState.addShape(new Shape(rex, rey, rew, reh, 'rgba(96, 96, 96, .5)', true));
}
}
myState.setIsDown(false);
};
this.start=function(){
canvas.addEventListener('selectstart',funSelectStart,false);
canvas.addEventListener('mousedown',funMouseDown, true);
canvas.addEventListener('mousemove',funMouseMove, true);
canvas.addEventListener('mouseup',funMouseUp,true);
};
this.end=function(){
canvas.removeEventListener('selectstart',funSelectStart,false);
canvas.removeEventListener('mousedown',funMouseDown, true);
canvas.removeEventListener('mousemove',funMouseMove, true);
canvas.removeEventListener('mouseup',funMouseUp,true);
};
setInterval(function() {
myState.draw();
},
myState.interval);
}
CanvasState.prototype.setIsDown = function(isDown) {
this.isDown = isDown;
}
CanvasState.prototype.setRect = function(rect) {
this.rect = rect;
}
CanvasState.prototype.addShape = function(shape) {
this.shapes.push(shape);
this.valid = false;
}
CanvasState.prototype.deleteShape = function(shape) {
this.shapes.splice($.inArray(shape, this.shapes), 1);
this.valid = false;
}
CanvasState.prototype.clear = function() {
this.ctx.clearRect(0, 0, this.width, this.height);
}
CanvasState.prototype.draw = function() {
if (!this.valid) {
var ctx = this.ctx;
var shapes = this.shapes;
this.clear();
var l = shapes.length;
for (var i = 0; i < l; i++) {
var shape = shapes[i];
if (shape.x > this.width || shape.y > this.height || shape.x + shape.w < 0 || shape.y + shape.h < 0) continue;
shapes[i].draw(ctx);
}
this.valid = true;
}
}
CanvasState.prototype.getMouse = function(e) {
var element = this.canvas,
offsetX = 0,
offsetY = 0,
mx, my;
if (element.offsetParent !== undefined) {
do {
offsetX += element.offsetLeft;
offsetY += element.offsetTop;
} while (( element = element . offsetParent ));
}
offsetX += this.stylePaddingLeft + this.styleBorderLeft + this.htmlLeft;
offsetY += this.stylePaddingTop + this.styleBorderTop + this.htmlTop;
mx = e.pageX - offsetX;
my = e.pageY - offsetY;
return {
x: mx,
y: my
};
}
需要引入jquery-1.10.2.min.js
图片:
效果如下:
1. 初始化 不可修改。
2. 点击start 可通过叉来消除,鼠标按下、移动、松开来创建,同时矩形可通过鼠标来移动。
3. 点击end 去除叉,同时去除鼠标事件。
4. 点击reset 返回初始页面。
5. 点击create
可创建多个canvas,同时可以通过按钮来控制事件。
初级版本仅供参考,后续有升级版本,添加功能按比例缩放。
相关推荐
一个HTML5 Canvas元素是一个矩形区域,可以通过JavaScript的Canvas API来获取这个区域的2D渲染上下文。通过这个上下文,我们可以执行一系列的绘图操作,如绘制线条、形状、图像以及复杂的动画。 在描述中提到的...
HTML5 Canvas是一个强大的图形绘制工具,它允许网页开发者在页面上动态绘制2D图形,而无需依赖外部插件。这个技术对于游戏开发、数据可视化、图表制作等应用场景非常有用。Canvas API提供了大量的方法和属性,使得...
HTML5的`canvas`元素是Web开发中用于动态图形绘制的重要工具,它允许开发者通过JavaScript进行复杂的图形操作,包括绘制线条、形状、文本以及处理图像。`canvas`本身并不具备绘图功能,而是作为一个图形容器,需要...
此外,为了提高用户体验,还可以添加一些优化,如缓存缩放后的图像,避免重复计算。 在提供的压缩包文件"delphi图片漫游框选放大滚轮缩放修正"中,可能包含了实现这些功能的源代码或示例项目。你可以通过查看和学习...
5. **SVG矢量图**:SVG是一种可缩放的矢量图形格式,HTML5支持内联SVG,可以创建高质量、分辨率不依赖的图形,适用于图标、图表等。 6. **Geolocation定位**:HTML5的Geolocation API允许网页获取用户的地理位置...
HTML5的Canvas是Web开发中用于动态图形绘制的重要组成部分,它允许开发者通过JavaScript直接在网页上进行图形操作。这篇指南将深入介绍Canvas的一些关键方法,帮助你更好地理解和运用这个强大的工具。 首先,Canvas...
在HTML5中,Canvas元素提供了一个强大的绘图接口,允许开发者动态绘制图形、文字和图像。当需要清除Canvas画布内容时,有两种主要方法:重置宽或高以及使用`clearRect`方法。 1. 重置宽或高 这种方法简单直接,...
Canvas是一个HTML5元素,通过JavaScript API,我们可以向其画布上添加像素数据,实现动态图像。它的工作原理是通过JavaScript函数来控制绘图,这些函数包括绘制线条、形状、图片、文字等,以及进行颜色处理和变换。 ...
HTML5中的`<canvas>`元素是用于图形渲染的强大工具。通过JavaScript,开发者可以直接控制页面上的像素,从而绘制图形、创建动画等。本次教程将详细介绍如何使用Canvas API进行图形变换和操作。 ##### 图形变换 - *...
5. **形状绘制**:canvas支持多种形状的绘制,如矩形、圆形、弧线等。例如,绘制一个填充为绿色的矩形: ```javascript ctx.fillRect(20, 20, 80, 60); ``` 6. **文本绘制**:canvas还可以添加文本,调整字体、...
Canvas是HTML5中引入的一个强大的绘图工具,它允许开发者在网页上动态绘制图形,创建交互式的视觉效果。以下是对24个基本的Canvas知识点的详细解析: 1. **填充矩形**: `fillRect(x, y, width, height)` 用于在指定...
// 如果矩形移动出画布,重置位置 if (x + 100 > canvas.width) { x = -100; } requestAnimationFrame(animate); } animate(); // 启动动画 ``` 此外,还可以利用canvas进行更复杂的动画,比如旋转、缩放、...
本文详细讲述了如何通过HTML5的画布(Canvas)和JavaScript(JS)实现旋转效果的示例。HTML5画布是一个非常强大的图形库,可以用于在网页上进行2D渲染。通过画布API,我们可以画出图形、画出文本,甚至进行像素级...
清除画布功能通常通过重置`Canvas`的背景色或者重新绘制一个全透明的矩形来实现。例如,可以创建一个全屏的矩形,颜色设置为透明(`Color.TRANSPARENT`),然后使用`canvas.drawRect()`方法覆盖整个画布,达到清除...
5. **动画效果**:为了让游戏更生动,我们可以添加一些简单的动画,如蛇移动时的平滑过渡。这可以通过缓存多帧图像并在合适的时间间隔内切换它们来实现,或者利用Canvas的translate()和scale()方法来实现简单的平移...
5. **还原与重置** 当需要清除所有变换并恢复到初始状态时,可以使用`reset()`方法。如果只是想撤销最后一步变换,可以调用`invert(matrix)`来获取逆矩阵,然后用这个逆矩阵进行一次新的变换。 6. **矩阵运算** ...
5. **还原变换**:`reset()` 方法可以将矩阵重置为单位矩阵,清除所有已设置的变换。 6. **合并变换**:`concat()` 方法可以将两个Matrix对象合并成一个新的变换。 7. **绘图操作**:在Canvas类中,`drawBitmap()`...
在Android开发中,Matrix类是用于处理二维图形变换的核心工具,它允许开发者对图像或View进行旋转、平移、缩放和倾斜等操作。本文将深入解析`android.matrix`,并结合提供的`MatrixDemo`来阐述相关知识点。 一、...
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> 便民设施系统 <meta http-equiv="content-type" content="text/html; charset=gbk"/> ...
- **操作方法**:按 `Ctrl` 可以快速选择矩形区域,按 `Ctrl + Alt` 可以添加到当前选区。使用 `V` 工具时,按 `Ctrl` 可以临时切换到移动工具,按 `Alt` 则可以缩放所选对象。 #### 技巧16:切换抓手工具 - **...