canvas 三 图片插入及文字填充

/**
 * canvas插入图片
 * 等图片加载完,在执行canvas操作
 * 		图片预加载:在onload 中调用方法
 * drawImage(oImg,x,y,w,h)
 * 		oImg:图片x,y坐标
 * 		w,h宽高
 * 
 * 设置背景
 * createPattern(oImg,平铺方式)
 * 		repeat,repeat-x,repeat-y,no-repeat
 * 
 * 渐变
 * 	createLinearGradient(x1,y1,x2,y2)
 * 	createRadialGradient(x1,y1,r1,x2,y2,r2)
 * 		第一个圆的坐标及半径
 * 
 * canvas填充文本
 * 	strokeText(文字,x,y) 文字边框的填充
 * 	fillText(文字,x,y)
 * 	font
 * 	textAlign
 * 	textBaseline
 * 	measureText()
 * 		measureText(str).width;只有宽度没有高度
 * 	阴影
 * 		shadowOffsetX shadowOffsetY
 * 	 	shadowBlur 高斯模糊值
 * 	 	shadowColor 阴影颜色
 */
var oc = document.getElementById("canvas");
var ogc = oc.getContext('2d');
//插入图片
var yImg = new Image();

	yImg.onload = function(){
		draw(this);
	}
	yImg.src = 'wennuan.jpg';

	function draw(obj){
		ogc.drawImage(obj,100,0);
	}
//颜色渐变
var obj = ogc.createLinearGradient(150,100,150,200);
obj.addColorStop(0,'red');
obj.addColorStop(1,'blue');
ogc.fillStyle = obj;
ogc.fillRect(150,100,100,100);
//径向渐变
var obj = ogc.createRadialGradient(200,200,100,200,100,150);
addColorStop(0,'red');
addColorStop(0.5,'yellow');
addColorStop(1,'blue');
ogc.fillStyle(obj);
ogc.fillRect(0,0,oc.width,oc.height);
//文字填充
ogc.font = '30px fontFamilu';//第二个参数必填
ogc.textBaseLine = 'top';	//top middle bottom
var w = ogc.measureText('文字');
ogc.fillText("文字",(oc.width-w)/2,(oc.height-60)/2); //文字居中水平垂直
ogc.strokeText('文字',0-,200);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值