wclarkson Posted May 16, 2018 Share Posted May 16, 2018 I'm finding the graphics examples very impressive, but it is the simple things that are tripping me up. I know I must be missing something simple, but is there a simple way to draw a rounded rectangle in Phaser 3? Link to comment Share on other sites More sharing options...
squilibob Posted May 18, 2018 Share Posted May 18, 2018 Not that I can find? I thought it would be a simple approach like passing in an array of points to make a polygon but it seems that it's up to the coder to use lineTo and arc to make their own rounded rect? You can make a helper function to do it manually. Starting from the middle of one of the sides you can construct it by either defining each of the corners in order all the way around or making an array that can programmatically do that work for you if you like the functional approach. The below demos the latter but it's computationally more expensive just for the sake of being functional programming: this.rounded = function(x, y, width, height, radius, color) { if (+width < radius * 2 || +height < radius * 2) return this.add.graphics({ fillStyle: { color: color}, x: +x, y: +y}) let options = Array.from({length: 4}, (_, index) => ({ radians: (index * Math.PI / 2 + Math.PI) % (2 * Math.PI), x: +x + (((index + 1) & 2) >> 1) * +width, y: +y + ~~(index > 1) * +height, lx: radius * ~~((index-2) * ((index-2) & 1)), ly: radius * ~~-((index + 1) & 1), ax: radius * (~((index + 1) & 2) + 2), ay: radius * (~~(index < 2) || -1) })) let shape = this.add.graphics({ fillStyle: { color: color}, x: +x, y: +y}) .beginPath() .moveTo(+x, +y + height >> 1) options.forEach((current, index, arr) => { shape .lineTo(current.x + current.lx, current.y + current.ly) .arc(current.x + current.ax, current.y + current.ay, +radius, current.radians, arr[index < arr.length - 1 ? index + 1 : 0].radians) }) return shape .closePath() .fillPath() } let myRect = this.rounded(50, 50, 400, 200, 20, 0xff8000) Link to comment Share on other sites More sharing options...
wclarkson Posted May 23, 2018 Author Share Posted May 23, 2018 Thanks! Yeah, it does seem like quite a bit for what used to be one line of code. I will take your function and try to adapt it to what I was doing. I wound up using a rectangle with two circles of the same color in the end to make a pill button. Link to comment Share on other sites More sharing options...
mazoku Posted June 17, 2018 Share Posted June 17, 2018 So still no rounded rectangle in Phaser 3? Link to comment Share on other sites More sharing options...
YuPi Posted June 18, 2018 Share Posted June 18, 2018 I've added a pull request for rounded rectangle on Friday, but it hasn't been merged yet. If everything is ok, then it will probably be in the next version. Link to comment Share on other sites More sharing options...
Bodman Posted August 3, 2018 Share Posted August 3, 2018 Much needed Link to comment Share on other sites More sharing options...
Juan Posted August 5, 2018 Share Posted August 5, 2018 Phaser 3 now has rounded rect support. https://labs.phaser.io/edit.html?src=src\game objects\graphics\fill rounded rectangle.js https://labs.phaser.io/edit.html?src=src\game objects\graphics\stroke rounded rectangle.js Link to comment Share on other sites More sharing options...
wclarkson Posted August 9, 2018 Author Share Posted August 9, 2018 Yes, I did see that turn up the other day on something. Great that it was added. Link to comment Share on other sites More sharing options...
Recommended Posts