Comments (4)
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 --
-
If you already have the
<canvas>
set up via canvas-sketch, you may not need to create another CanvasSpace. You can pass the rendering context directly to CanvasForm, egnew CanvasForm( ctx )
-
If you are looking for a way to generate high-quality prints, you can try node-pts-canvas. Note that this is experimental and may not work 100%.
Hope this helps!
from pts.
Thank you for the response, @williamngan !
As you suggested, I am just using CanvasForm
although I wish I can use convenience methods and properties that come with CanvasSpace
. If only there is a way to modify my canvas to do that...
Also when using new CanvasForm(ctx)
, it seems that the gradient is not supported?
I was recreating one of your demos - CanvasForm.gradient, and the gradient does not work properly.
I get an error:
Uncaught TypeError: Cannot read properties of undefined (reading 'ctx')
at get ctx [as ctx] (dist.js:3146:26)
at dist.js:3209:33
at dist.js:5283:13
at dist.js:5288:3
Sample code below. This time, I am using pts-starter-kit
and do not have any other dependency.
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));
}
from pts.
Thanks for the report! I will take a look soon.
This is a common enough use case, so maybe we should create a CustomCanvasSpace
or something similar for this type of use case.
from pts.
Thank you for looking into this.
the gradient issue is now fixed with 0.10.12
as mentioned in the other issue.
from pts.
Related Issues (20)
- source-map file missing when importing pts
- Fix parameter types HOT 1
- CanvasForm(ctx) methods error. HOT 3
- Implement oklab color
- will pts provide an api to render in existing canvas provided by the user? HOT 2
- pts.js, pts.min.js added to .npmignore and are not served by cdns any longer HOT 2
- Documentation is unclear on how to "unmount" a canvas. HOT 2
- Radial gradients can't use negative coordinates HOT 2
- Safari 15 // Group.moveTo not reflected HOT 5
- Crisp 1px lines on HiDPI devices. HOT 1
- Canvas mouse events coord is wrong on layout change HOT 1
- Add support for roundRect method. HOT 1
- randomPt overwrites input value
- error on form methods when using with an existing canvas HOT 2
- Drawing tools
- Drawing tools
- Add brush like this one https://p5-brush.cargo.site/? HOT 1
- Built-in SVG interactions HOT 2
- Request: Variable thickness lines/curves
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pts.