
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()
</script>
三、绘制小人行走的帧动画
<s<script>
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,<br>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()
</script>
四、绘制疾走的小怪兽
可以通过键盘上下左右键控制小人在画布中任意行走
nction(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()
</script>
深圳网站建设www.sz886.com
mental canvas可以导入图片。
画布用canvas作画,首先,你需要有一块“画布”。如果你的书架里面没有画布,你可以买一卷回来放进去。当然,在网页里面我们不需要花钱买,直接写一个canvas即可。
接着讲解“插入图片和设置”的四个步骤:插入图片——调整图片的大小——设置图片版式——移动图片,然后总结 *** 作步骤,最后让学生大显身手。
mental canvas设计思路:
一般情况下,人们对于word文档的使用仅仅是使用了编写文字的一部分作用,其中很多的功能都没忽略了。使用图片来进行美化,就是其中一个功能。我们先设计教学情境,在作文中插入图片,美化作文,也使作文的感染力大大加强。进而通过本微课,学习图片的插入方法。
方案只有一种:
1、图片转换为Bitmap对象
2、通过canvas的drawBitmap方法绘制图片对象
示例:
1、图片转换成Bitmap对象1)资源文件转换
Bitmap bmp=BitmapFactory.decodeResource(r, R.drawable.icon)//读取drawable下的icon图片,转换为bitmap对象
2)根据路径转换
public Bitmap convertToBitmap(String path, int w, int h) {
BitmapFactory.Options opts = new BitmapFactory.Options()
opts.inJustDecodeBounds = true// 设置为ture只获取图片大小
opts.inPreferredConfig = Bitmap.Config.ARGB_8888//颜色值
BitmapFactory.decodeFile(path, opts)//返回为空,opts返回图片大小。
int width = opts.outWidth//图片实际宽度
int height = opts.outHeight//图片实际高度
float scaleWidth = 0.f, scaleHeight = 0.f
if (width > w || height > h) {//缩放图片
// 缩放
scaleWidth = ((float) width) / w
scaleHeight = ((float) height) / h
}
opts.inJustDecodeBounds = false//设置缩放图片
float scale = Math.max(scaleWidth, scaleHeight)
opts.inSampleSize = (int)scale//设置缩放比例
WeakReference<Bitmap> weak = new WeakReference<Bitmap>(BitmapFactory.decodeFile(path, opts))//获取图片的弱引用,便于释放图片占用内存
return Bitmap.createScaledBitmap(weak.get(), w, h, true)//返回图片对象
}
2、canvas上绘制图片
Bitmap bmp//获取的bitmap对象
Paint p//定义画笔
canvasTemp.drawBitmap(bmp, 50, 50, p)//在50,50位置绘制图片
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)