-
Notifications
You must be signed in to change notification settings - Fork 182
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
Canvas size issue when html attribute and css style width & height differs. #184
Comments
Hi @cdaein - yes, the css styles and width/height attributes differ to support different pixel density screens automatically. Eg, if you're using a 2x retina screen, the canvas' actual size will be double of the width & height. I'm not familiar with canvas-sketch, but here are a couple suggestions --
Hope this helps! |
Thank you for the response, @williamngan ! Also when using I get an error:
Sample code below. This time, I am using import { CanvasForm, Pt, Bound, Create, Circle } from "pts";
// prepare canvas
const canvas = document.createElement("canvas");
canvas.width = 600;
canvas.height = 600;
document.body.appendChild(canvas);
const ctx = canvas.getContext("2d");
const form = new CanvasForm(ctx);
// recreate CanvasSpace properties for demonstration
const center = new Pt(canvas.width / 2, canvas.height / 2);
const size = new Pt(canvas.width, canvas.height);
const innerBound = new Bound(new Pt(), size);
// simulate mouse pointer
const pointer = new Pt(100, 100);
let scale = center.$subtract(pointer).divide(center).abs();
let bound = new Bound(new Pt(), size.$add(0, size.y * scale.y));
let cells = Create.gridCells(bound, 21, 30);
let offy = (bound.height - innerBound.height) / 2;
let cy = 1 - Math.abs(center.y - pointer.y) / center.y;
let radial = form.gradient([
[0.2, `rgba(${70 * cy}, 0, ${255 * cy})`],
[0.6, `rgba(${205 * cy}, 0, ${30 * cy})`],
[0.95, `rgba(${255 * cy}, ${220 * cy}, 0)`],
]);
// radial gradient seems to have an issue with 'ctx'
form
.fill(
radial(
Circle.fromCenter(pointer, center.y / 2),
Circle.fromCenter(pointer, size.y * 1.5)
)
)
.rect(innerBound);
for (let i = 0, len = cells.length; i < len; i++) {
let grad = form.gradient(["rgba(255,255,255,1)", "rgba(255,255,255,0)"]);
form
.fillOnly(i % 2 === 0 ? grad(cells[i]) : "rgba(0,0,0,0)")
.rect(cells[i].subtract(0, offy));
} |
Thanks for the report! I will take a look soon. This is a common enough use case, so maybe we should create a |
Thank you for looking into this. |
Hi,
I'm trying to use pts.js with canvas-sketch, which provides its own canvas and context.
When I use it with the
CanvasForm
object only, there is no problem.But when I add
CanvasSpace
supplying the existing canvas, there is a canvas sizing issue.I think this happens when
<canvas>
width & height html attribute is different from width & height css style value.canvas-sketch
automatically resizes the canvas css w&h according to the current browser window size.I haven't found a way to update pts.js code accordingly. If anyone has a suggestion, I'd appreciate it.
Here is my sample code to reproduce the problem:
The text was updated successfully, but these errors were encountered: