-
Notifications
You must be signed in to change notification settings - Fork 11.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor helpers.canvas.drawPoint() #5623
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,8 @@ var exports = module.exports = { | |
ctx.arcTo(x, y + height, x, y + height - r, r); | ||
ctx.lineTo(x, y + r); | ||
ctx.arcTo(x, y, x + r, y, r); | ||
ctx.closePath(); | ||
ctx.moveTo(x, y); | ||
} else { | ||
ctx.rect(x, y, width, height); | ||
} | ||
|
@@ -65,77 +67,61 @@ var exports = module.exports = { | |
ctx.save(); | ||
ctx.translate(x, y); | ||
ctx.rotate(rotation * Math.PI / 180); | ||
ctx.beginPath(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
switch (style) { | ||
// Default includes circle | ||
default: | ||
ctx.beginPath(); | ||
ctx.arc(0, 0, radius, 0, Math.PI * 2); | ||
ctx.closePath(); | ||
ctx.fill(); | ||
break; | ||
case 'triangle': | ||
ctx.beginPath(); | ||
edgeLength = 3 * radius / Math.sqrt(3); | ||
height = edgeLength * Math.sqrt(3) / 2; | ||
ctx.moveTo(-edgeLength / 2, height / 3); | ||
ctx.lineTo(edgeLength / 2, height / 3); | ||
ctx.lineTo(0, -2 * height / 3); | ||
ctx.closePath(); | ||
ctx.fill(); | ||
break; | ||
case 'rect': | ||
size = 1 / Math.SQRT2 * radius; | ||
ctx.beginPath(); | ||
ctx.fillRect(-size, -size, 2 * size, 2 * size); | ||
ctx.strokeRect(-size, -size, 2 * size, 2 * size); | ||
ctx.rect(-size, -size, 2 * size, 2 * size); | ||
break; | ||
case 'rectRounded': | ||
var offset = radius / Math.SQRT2; | ||
var leftX = -offset; | ||
var topY = -offset; | ||
var sideSize = Math.SQRT2 * radius; | ||
ctx.beginPath(); | ||
|
||
// NOTE(SB) the rounded rect implementation changed to use `arcTo` | ||
// instead of `quadraticCurveTo` since it generates better results | ||
// when rect is almost a circle. 0.425 (instead of 0.5) produces | ||
// results visually closer to the previous impl. | ||
this.roundedRect(ctx, leftX, topY, sideSize, sideSize, radius * 0.425); | ||
|
||
ctx.closePath(); | ||
ctx.fill(); | ||
break; | ||
case 'rectRot': | ||
size = 1 / Math.SQRT2 * radius; | ||
ctx.beginPath(); | ||
ctx.moveTo(-size, 0); | ||
ctx.lineTo(0, size); | ||
ctx.lineTo(size, 0); | ||
ctx.lineTo(0, -size); | ||
ctx.closePath(); | ||
ctx.fill(); | ||
break; | ||
case 'cross': | ||
ctx.beginPath(); | ||
ctx.moveTo(0, radius); | ||
ctx.lineTo(0, -radius); | ||
ctx.moveTo(-radius, 0); | ||
ctx.lineTo(radius, 0); | ||
ctx.closePath(); | ||
break; | ||
case 'crossRot': | ||
ctx.beginPath(); | ||
xOffset = Math.cos(Math.PI / 4) * radius; | ||
yOffset = Math.sin(Math.PI / 4) * radius; | ||
ctx.moveTo(-xOffset, -yOffset); | ||
ctx.lineTo(xOffset, yOffset); | ||
ctx.moveTo(-xOffset, yOffset); | ||
ctx.lineTo(xOffset, -yOffset); | ||
ctx.closePath(); | ||
break; | ||
case 'star': | ||
ctx.beginPath(); | ||
ctx.moveTo(0, radius); | ||
ctx.lineTo(0, -radius); | ||
ctx.moveTo(-radius, 0); | ||
|
@@ -146,22 +132,18 @@ var exports = module.exports = { | |
ctx.lineTo(xOffset, yOffset); | ||
ctx.moveTo(-xOffset, yOffset); | ||
ctx.lineTo(xOffset, -yOffset); | ||
ctx.closePath(); | ||
break; | ||
case 'line': | ||
ctx.beginPath(); | ||
ctx.moveTo(-radius, 0); | ||
ctx.lineTo(radius, 0); | ||
ctx.closePath(); | ||
break; | ||
case 'dash': | ||
ctx.beginPath(); | ||
ctx.moveTo(0, 0); | ||
ctx.lineTo(radius, 0); | ||
ctx.closePath(); | ||
break; | ||
} | ||
|
||
ctx.fill(); | ||
ctx.stroke(); | ||
ctx.restore(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any impact (in the calling code) to not call |
||
}, | ||
|
@@ -224,5 +206,4 @@ helpers.clear = exports.clear; | |
helpers.drawRoundedRectangle = function(ctx) { | ||
ctx.beginPath(); | ||
exports.roundedRect.apply(exports, arguments); | ||
ctx.closePath(); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's also written that
rect()
"must then create a new subpath with the point (x, y) as the only point in the subpath". Shoud we addctx.move(x, y)
right afterclosePath()
or do you think it's not application to a rounded rectangle?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ctx.move(x, y)
afterclosePath()
makes sense. I will update the code.