html在图片上画圈保存,html5 canvas 画矩形、画线、画圆、渐变背景、文字、图片...

canvas id="myCanvas" width="1000" height="600" style="border:1px

solid #c3c3c3;">

script type="text/javascript">

var c=document.getElementByIdx_x("myCanvas");

var cxt=c.getContext("2d");

//画矩形

cxt.fillStyle="#FF0000";

cxt.fillRect(0,0,150,75);

//画线

cxt.moveTo(10,10);

cxt.lineTo(150,50);

cxt.lineTo(10,50);

cxt.stroke();

//画圆

cxt.fillStyle="#00FF00";

cxt.beginPath();

cxt.arc(70,18,15,0,Math.PI*2,true);

cxt.closePath();

cxt.fill();

//渐变背景

var grd=cxt.createLinearGradient(200,0,175,50);

grd.addColorStop(0,"#FF0000");

grd.addColorStop(1,"#00FF00");

cxt.fillStyle=grd;

cxt.fillRect(200,0,175,50);

//文字

cxt.font = "20px Georgia";

cxt.fillText("Hello World!", 10, 50);

//渐变文字

cxt.font = "30px Verdana";

// 创建渐变

var gradient = cxt.createLinearGradient(0, 0, 200, 0);

gradient.addColorStop("0", "magenta");

gradient.addColorStop("0.5", "blue");

gradient.addColorStop("1.0", "red");

// 用渐变填色

cxt.fillStyle = gradient;

cxt.fillText("longtianyu1.blog.163.com", 10, 90);

//镂空文字

cxt.strokeText("Hello World!", 10, 130);

//镂空渐变文字

cxt.strokeStyle = gradient;

cxt.strokeText("longtianyu1.blog.163.com", 10, 170);

//图片

var img=new Image()

img.src="flower.png"

cxt.drawImage(img,200,0);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 中可以使用 HTML5Canvas API 来实现在页面,可以通过在 Vue 组件中添加 canvas 元素,并在 mounted 钩子函数中获取其 2D 上下文来实现。 以下是一个实现在页面的示例代码: ```html <template> <div> <canvas ref="canvas" @mousedown="startDrawing" @mousemove="draw" @mouseup="stopDrawing"></canvas> </div> </template> <script> export default { name: 'DrawCanvas', data() { return { isDrawing: false, lastX: 0, lastY: 0, ctx: null, } }, mounted() { this.ctx = this.$refs.canvas.getContext('2d'); }, methods: { startDrawing(e) { this.isDrawing = true; this.lastX = e.offsetX; this.lastY = e.offsetY; }, draw(e) { if (!this.isDrawing) return; this.ctx.beginPath(); this.ctx.moveTo(this.lastX, this.lastY); this.ctx.lineTo(e.offsetX, e.offsetY); this.ctx.stroke(); this.lastX = e.offsetX; this.lastY = e.offsetY; }, stopDrawing() { this.isDrawing = false; }, }, } </script> <style scoped> canvas { width: 100%; height: 100%; border: 1px solid #000; } </style> ``` 上面的代码中,我们在组件中添加了一个 canvas 元素,并为其添加了三个事件监听器,分别是 @mousedown、@mousemove 和 @mouseup,用于开始、绘制和停止绘制。 在 mounted 钩子函数中,我们获取了 canvas 元素的 2D 上下文,并将其保存在组件的 this.ctx 属性中。 在组件的 methods 中,我们定义了三个方法,分别是 startDrawing、draw 和 stopDrawing。 startDrawing 方法用于在鼠标按下时记录当前鼠标的位置,并将 isDrawing 属性设置为 true。 draw 方法用于在鼠标移动时绘制线条,如果 isDrawing 为 true,则使用 moveTo 和 lineTo 方法绘制一条线段,并将 lastX 和 lastY 属性更新为当前鼠标的位置。 stopDrawing 方法用于在鼠标松开时将 isDrawing 属性设置为 false,停止绘制。 最后,我们在组件的 template 中使用 ref 属性来获取 canvas 元素,并为其添加样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值