设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 105|回复: 4
打印 上一主题 下一主题

[有事请教] 神金 有通用的圆角方法吗

[复制链接]

Lv4.逐梦者

梦石
0
星屑
5315
在线时间
58 小时
注册时间
2024-8-28
帖子
32
跳转到指定楼层
1
发表于 前天 16:08 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
RUBY 代码复制
  1. <canvas id="myCanvas" width="400" height="400" style="border:1px solid #000000;"></canvas>
  2. <script>
  3.   function drawRoundedRect(ctx, x, y, width, height, radius) {
  4.     ctx.beginPath();
  5.     ctx.moveTo(x + radius, y); // Move to the starting point
  6.     ctx.lineTo(x + width - radius, y);
  7.     ctx.arcTo(x + width, y, x + width, y + height, radius);
  8.     ctx.lineTo(x + width, y + height - radius);
  9.     ctx.arcTo(x + width, y + height, x + width - radius, y + height, radius);
  10.     ctx.lineTo(x + radius, y + height);
  11.     ctx.arcTo(x, y + height, x, y + height - radius, radius);
  12.     ctx.lineTo(x, y + radius);
  13.     ctx.arcTo(x, y, x + radius, y, radius);
  14.  
  15.     ctx.closePath();
  16.     ctx.fill();
  17.     ctx.stroke();
  18.   }
  19.  
  20.   const canvas = document.getElementById('myCanvas');
  21.   const ctx = canvas.getContext('2d');
  22.  
  23.   drawRoundedRect(ctx, 50, 50, 300, 200, 100); // Draw a rounded rectangle
  24. </script>

这是正常的圆角代码 画出来是这样







RUBY 代码复制
  1. Bitmap.prototype.fillRoundedRect = function(x, y, width, height, cornerRadius, color) {
  2.     const context = this.context;
  3.     context.save();
  4.     context.fillStyle = color;
  5.     if (cornerRadius === 0) {
  6.         context.fillRect(x, y, width, height);
  7.     } else {
  8.         const radius = Math.min(cornerRadius, width / 2, height / 2);
  9.         context.beginPath();
  10.         context.moveTo(x + radius, y);
  11.         context.lineTo(x + width - radius, y);
  12.         context.arcTo(x + width, y, x + width, y + height, radius);
  13.         context.lineTo(x + width, y + height - radius);
  14.         context.arcTo(x + width, y + height, x + width - radius, y + height, radius);
  15.         context.lineTo(x + radius, y + height);
  16.         context.arcTo(x, y + height, x, y + height - radius, radius);
  17.         context.lineTo(x, y + radius);
  18.         context.arcTo(x, y, x + radius, y, radius);
  19.         context.fill();
  20.     }
  21.     context.restore();
  22.     this._baseTexture.update();
  23. };

我给扔到bitmap里



爆了 不想看这个鬼东西了  来个能用的
堆设区
高雅的音乐 黑白的画面 喜剧的形式 悲剧的内核 意外的结局 JRPG卓别林

Lv3.寻梦者

梦石
0
星屑
2429
在线时间
400 小时
注册时间
2020-8-20
帖子
71
2
发表于 前天 18:38 | 只看该作者
本帖最后由 pkeasygod 于 2024-9-25 20:18 编辑

刚刚测试错误,修正一下

bitmap的width及height 要大于 图形的 x+width y+height
把bitmap的尺寸改大大大 好像直接解决问题

(已改4次)应该没错了吧

点评

原来有内置的圆角 - -  发表于 昨天 17:46
https://developer.mozilla.org/zh-CN/docs/Web/API/CanvasRenderingContext2D/roundRect 按理说可以直接用这个的,chromium99版本以上都可以。  发表于 昨天 08:47
回复 支持 反对

使用道具 举报

Lv2.观梦者

梦石
0
星屑
723
在线时间
105 小时
注册时间
2024-5-22
帖子
77
3
发表于 昨天 17:31 | 只看该作者
js里面有个roundRect方法画圆角矩形的,楼主可以查一下
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-9-27 06:50

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表