Canvas实现圆角图片实例页面

HTML代码:
<strong>这是原图</strong>
<p><img src="http://image.zhangxinxu.com/image/study/s/s256/mm1.jpg" width="256" height="191"></p>
<strong>这是Canvas实现</strong>
<p>圆角大小:<span class="range-txt"> 0 <input id="radiusInput" type="range" min="0" max="92" value="92"> 92</span></p>
<canvas id="canvas" width="256" height="191"></canvas>


JS代码:
//圆角矩形
CanvasRenderingContext2D.prototype.roundRect = function (x, y, w, h, r) {
    var min_size = Math.min(w, h);
    if (r > min_size / 2) r = min_size / 2;
    // 开始绘制
    this.beginPath();
    this.moveTo(x + r, y);
    this.arcTo(x + w, y, x + w, y + h, r);
    this.arcTo(x + w, y + h, x, y + h, r);
    this.arcTo(x, y + h, x, y, r);
    this.arcTo(x, y, x + w, y, r);
    this.closePath();
    return this;
}

// canvas元素, 图片元素
var canvas = document.querySelector("#canvas"), image = new Image(), input = document.getElementById("radiusInput");
var context = canvas.getContext("2d");

var draw = function(obj) {
    // 创建图片纹理
    var pattern = context.createPattern(obj, "no-repeat");
    // 如果要绘制一个圆,使用下面代码
    // context.arc(obj.width / 2, obj.height / 2, Math.max(obj.width, obj.height) / 2, 0, 2 * Math.PI);
    // 这里使用圆角矩形
    context.roundRect(0, 0, obj.width, obj.height, input.value * 1 || 0);
    
    // 填充绘制的圆
    context.fillStyle = pattern;
    context.fill();    
}

image.onload = function() {
    draw(this);
};
image.src = "http://image.zhangxinxu.com/image/study/s/s256/mm1.jpg";

input.addEventListener("change", function() {
    context.clearRect(0, 0, canvas.width, canvas.height);
    draw(image);	
});
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过使用 `canvas` 的 `drawImage()` 方法和 `arc()` 方法来绘制一个带有圆角图片。首先,需要将图片加载到 `canvas` 上,然后使用 `arc()` 方法绘制圆角。具体步骤如下: 1. 创建一个 `canvas` 元素和一个 `image` 元素,并将图片加载到 `image` 元素。 ```html <canvas id="myCanvas"></canvas> <img id="myImage" src="path/to/image"> ``` 2. 在 JavaScript 获取 `canvas` 和 `image` 元素,并将图片绘制到 `canvas` 上。 ```javascript const canvas = document.getElementById("myCanvas"); const ctx = canvas.getContext("2d"); const img = document.getElementById("myImage"); ctx.drawImage(img, 0, 0); ``` 3. 使用 `arc()` 方法绘制圆角。该方法需要指定圆心坐标、半径、起始角度和结束角度。 ```javascript const radius = 20; const x = 0; const y = 0; const width = canvas.width; const height = canvas.height; ctx.beginPath(); ctx.moveTo(x + radius, y); ctx.lineTo(x + width - radius, y); ctx.arcTo(x + width, y, x + width, y + radius, radius); ctx.lineTo(x + width, y + height - radius); ctx.arcTo(x + width, y + height, x + width - radius, y + height, radius); ctx.lineTo(x + radius, y + height); ctx.arcTo(x, y + height, x, y + height - radius, radius); ctx.lineTo(x, y + radius); ctx.arcTo(x, y, x + radius, y, radius); ctx.closePath(); ctx.clip(); ``` 4. 最后,将绘制好的圆角图片保存到变量或者直接在 `canvas` 上显示。 ```javascript const roundedImg = canvas.toDataURL(); ``` 完整代码示例: ```html <canvas id="myCanvas"></canvas> <img id="myImage" src="path/to/image"> <script> const canvas = document.getElementById("myCanvas"); const ctx = canvas.getContext("2d"); const img = document.getElementById("myImage"); ctx.drawImage(img, 0, 0); const radius = 20; const x = 0; const y = 0; const width = canvas.width; const height = canvas.height; ctx.beginPath(); ctx.moveTo(x + radius, y); ctx.lineTo(x + width - radius, y); ctx.arcTo(x + width, y, x + width, y + radius, radius); ctx.lineTo(x + width, y + height - radius); ctx.arcTo(x + width, y + height, x + width - radius, y + height, radius); ctx.lineTo(x + radius, y + height); ctx.arcTo(x, y + height, x, y + height - radius, radius); ctx.lineTo(x, y + radius); ctx.arcTo(x, y, x + radius, y, radius); ctx.closePath(); ctx.clip(); const roundedImg = canvas.toDataURL(); </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值