一、在canvas画布中如何加载图片

---用drawImage( )方法

drawImage用法的三种情况:

1、在画布指定位置定义图像

ctx.drawImage(img,x,y);

注:此时画布上显示的图片大小是图片的默认大小

2、在画布上定位图像,并规定图像的宽度和高度:

ctx.drawImage(img,x,y,width,height);

3、剪切图像,并在画布上定位被剪切的部分:

ctx.drawImage(img,sx,sy,swidth,sheight,x,y,width,height);

参数值

4、加载图片的两种方法

(1)、document.getElementById(' img ');

(2)、var image = new Image( ) ;

二、在画布中心加载部分图片

如上图、加载第一行的第一个小怪兽

var Person = function(canvas){

this.ctx = document.querySelector("canvas").getContext("2d");

this.canvasWidth = this.ctx.canvas.width;

this.canvasHeight = this.ctx.canvas.height;

this.src = "image/04.png";

this.init();

}

Person.prototype.init = function(){

var that = this;

this.loadImage(function(image){

// 获取图片的宽高

that.imageWidth = image.width;

that.imageHeight = image.height;

// 获取单个小怪兽区域的宽高

that.positionWidth = that.imageWidth / 4;

that.positionHeight = that.imageHeight / 4;

// 默认是从左上角显示的

that.x0 = that.canvasWidth / 2 - that.positionWidth / 2;

that.y0 = that.canvasHeight / 2 - that.positionHeight / 2;

// 绘制图片

that.ctx.drawImage(image, 0, 0, that.positionWidth, that.positionHeight, that.x0,

that.y0, that.positionWidth, that.positionHeight);

})

}

Person.prototype.loadImage = function(callback){

var image = new Image();

// 图片加载完成后执行

image.onload = function(){

callback && callback(image);

}

image.src = this.src;

}

var person = new Person();

三、绘制小人行走的帧动画

var Person = function(canvas){

this.ctx = document.querySelector("canvas").getContext("2d");

this.canvasWidth = this.ctx.canvas.width;

this.canvasHeight = this.ctx.canvas.height;

this.src = "image/04.png";

this.init();

}

Person.prototype.init = function(){

var that = this;

this.loadImage(function(image){

// 获取图片的宽高

that.imageWidth = image.width;

that.imageHeight = image.height;

// 获取单个小怪兽区域的宽高

that.positionWidth = that.imageWidth / 4;

that.positionHeight = that.imageHeight / 4;

// 默认是从左上角显示的

that.x0 = that.canvasWidth / 2 - that.positionWidth / 2;

that.y0 = that.canvasHeight / 2 - that.positionHeight / 2;

// 绘制图片

that.ctx.drawImage(image, 0, 0, that.positionWidth, that.positionHeight, that.x0,

that.y0, that.positionWidth, that.positionHeight);

var index = 0;

setInterval(function(){

that.ctx.clearRect(0, 0, that.canvasWidth, that.canvasHeight);

index++;

that.ctx.drawImage(image, index * that.positionWidth, 0, that.positionWidth, that.positionHeight, that.x0, that.y0,

that.positionWidth, that.positionHeight);

if(index >= 3){

index = 0;

}

}, 100);

})

}

Person.prototype.loadImage = function(callback){

var image = new Image();

// 图片加载完成后执行

image.onload = function(){

callback && callback(image);

}

image.src = this.src;

}

var person = new Person();

四、绘制疾走的小怪兽

可以通过键盘上下左右键控制小人在画布中任意行走

<

var Person = function(canvas){

this.ctx = document.querySelector("canvas").getContext("2d");

this.canvasWidth = this.ctx.canvas.width;

this.canvasHeight = this.ctx.canvas.height;

this.stepX = 0;

this.stepY = 0;

this.stepSize = 10;

this.index = 0;

this.direction = 0;

this.src = "image/04.png";

this.init();

}

Person.prototype.init = function(){

var that = this;

this.loadImage(function(image){

// 获取图片的宽高

that.imageWidth = image.width;

that.imageHeight = image.height;

// 获取单个小怪兽区域的宽高

that.positionWidth = that.imageWidth / 4;

that.positionHeight = that.imageHeight / 4;

// 默认是从左上角显示的

that.x0 = that.canvasWidth / 2 - that.positionWidth / 2;

that.y0 = that.canvasHeight / 2 - that.positionHeight / 2;

// 绘制图片

that.ctx.drawImage(image, 0, 0, that.positionWidth, that.positionHeight, that.x0,

that.y0, that.positionWidth, that.positionHeight);

var index = 0;

document.onkeydown = function(e){

that.ctx.clearRect(0, 0, that.canvasWidth, that.canvasHeight);

switch(e.keyCode){

case 37 :

console.log('左');

that.direction = 1;

that.stepX--;

that.showImage(image);

break;

case 38 :

console.log('上');

that.direction = 3;

that.stepY--;

that.showImage(image);

break;

case 39 :

console.log('右');

that.direction = 2;

that.stepX++;

that.showImage(image);

break;

case 40 :

console.log('下');

that.direction = 0;

that.stepY++;

that.showImage(image);

break;

}

}

})

}

Person.prototype.loadImage = function(callback){

var image = new Image();

// 图片加载完成后执行

image.onload = function(){

callback && callback(image);

}

image.src = this.src;

}

Person.prototype.showImage = function(image){

this.index++;

console.log(this.index);

this.ctx.drawImage(image, this.index * this.positionWidth, this.direction * this.positionHeight, this.positionWidth, this.positionHeight, this.x0 + this.stepX * this.stepSize, this.y0 + this.stepY * this.stepSize, this.positionWidth, this.positionHeight);

if(this.index >= 3){

this.index = 0;

}

}

var person = new Person();

java的drawimage_canvas中的drawImage相关推荐

  1. java中 g.drawImage()方法如何使用

    java中 g.drawImage()方法如何使用 2013-04-08 09:04 contact999  |  分类:JAVA相关  |  浏览1921次 File file=new File(& ...

  2. 优化Java动画编程中的显示效果

    优化Java动画编程中的显示效果 作者:赵福军 曹代勇 李青云 发文时间:2002.11.12 09:32:33 Java动画编程有多种实现方法,但它们实现的基本原理是一样的,即在屏幕上画出一系列的帧 ...

  3. Java类似相机中图像处理(上)

    Java类似相机中图像处理(上) 一.前情提要 二.项目流程 \qquad 1. 创建窗体 \qquad 2. 动作监听器 \qquad 3. 获取图片文件像素值 \qquad 4. 画原画 \qqu ...

  4. java从字符串中提取数字

    1.做一下操作时会一般会用到提取数字操纵: a.列表中有翻页,当新添加的数据不是放在第一条或者最后一条时,需要翻页并循环找到对应的那条数据 b.当新添加的数据放在第一条或者最后一条时,则不需要翻页,只 ...

  5. JAVA Web项目中所出现错误及解决方式合集(不断更新中)

    JAVA Web项目中所出现错误及解决方式合集 前言 一.几个或许会用到的软件下载官网 二.Eclipse的[preferences]下没有[sever]选项 三.Tomcat的安装路径找不到 四.T ...

  6. Java类Demo中存在_Java中的数据类型转换

    先来看一个题: Java类Demo中存在方法func0.func1.func2.func3和func4,请问该方法中,哪些是不合法的定义?( ) public class Demo{ float fu ...

  7. Java数据结构一 —— Java Collections API中的表

    1.Collection接口 位于java.util包中,以下是重要的部分. 1 public interface Collection<AnyType> extends Iterable ...

  8. Java之戳中痛点 - (4)i++ 和 ++i 探究原理

    先看一个例子: package com.test;public class AutoIncrement {public static void main(String[] args) {int a=7 ...

  9. IDEA报错解决:Error:(33, 35) java: -source 7 中不支持 lambda 表达式 (请使用 -source 8 或更高版本以启用 lambda 表达式)

    晚上在用IDEA的时候遇到了报错: Error:(33, 35) java: -source 7 中不支持 lambda 表达式(请使用 -source 8 或更高版本以启用 lambda 表达式) ...

最新文章

  1. tensowflow 训练 远程提交_深度解析AutoML工具——NNI:带上超参一起训练
  2. 皮一皮:这才是持之以恒的专一...
  3. 自顶向下 与自底向上解决01 背包问题
  4. 【乱侃】How do they look them ?
  5. 通俗理解Paxos算法
  6. 《Python编程从入门到实践》记录之Python处理CSV文件数据
  7. Test SRM Level Two: CountExpressions, Brute Force
  8. iOS 数组模型排序
  9. 人月神话阅读笔记(2)
  10. 100个最受欢迎的机器学习课程视频
  11. Atitit 手机图片备份解决方案attilax总结
  12. MATLAB-SIMULINK-二极管搭建整流电路(1)
  13. java判断日期是否是同一周_java中如何判断两个日期是否是同一周
  14. Debussy软件的脚本调用
  15. 计算机网络 路由器的端口ip,教您如何修改路由器LAN端口的IP地址
  16. Delphi中类的VMT
  17. zbrush软件介绍
  18. Activiti6:模拟钉钉上面的请假流程(使用web画图并导出xml然后使用java执行流程)
  19. Kubernetes监控在小米的落地
  20. 预防死锁,检测死锁,避免死锁,解除死锁....

热门文章

  1. 什么是PoE、PSE、PD设备?
  2. VBA添加Excel工作表
  3. exrpg服务器维护,[娱乐|综合][SCT]VexRPGassistant——基于VV的RPG助手,提升服务器逼格吧[1.9-1.12.2]...
  4. brother兄弟机床数控系统cnc采集方案Tcp通讯
  5. Nginx服务器搭建与个人博客部署
  6. java服务 重启_Java服务重新启动4或5次
  7. 用FME2012征服LiDAR
  8. MapReduce编程(五) 单表关联
  9. kdevelop怎么调试_使用Kdevelop4调试ns
  10. 怎样利用github搭建个人博客