Let's say I have drawn a rectangle on my canvas and I want to clean it in order to draw some other figure / polygon / arc ....
How can I do it? I have tried it in many ways but none has worked.
I think this may work but I'm not sure:
GraphicsContext gc = myCanvas.getGraphicsContext2D();
gc.setFill(Color.ALICEBLUE);
gc.fillRect(0, 0, 300, 200);
Could you tell me if this will work consistently and whether it is the standard way to achieve this goal?
The method clearRect seems to be dedicated for this:
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
Related
For neuroscience research I'm attempting to train rats to press shapes on a touch screen. Ideally, these shapes would be highly distinct polygons or blobs to make it easier for the rats to discriminate. However, to limit some biases toward certain shapes, I'd like to keep the area of each shape equivalent. I've been trying to achieve this on p5.js, but I'm very new to this.
The code I've got so far provides some of the shape randomness, but not the consitency in area:
function setup() {
createCanvas(500, 500);
background(255);
fill(0);
translate(width/2, height/2);
beginShape();
for(let i = 0; i < 5; i++) {
const x = random(-250, 250);
const y = random(-250, 250);
vertex(x, y);
endShape();
}
}
Any help achieving this would be very appreciated
Maybe it's not what you need, the shapes are all inscribed in a circle(edit:the second code ones), not a bounding box as I said earlier.
Both examples are online at p5js editor:
The code I had was more free form, it will eternally increase the number of sides:
about 20 sides:
about 130 sides
But at the begining its more what I think you need
the code is here:
https://editor.p5js.org/v-k-/sketches/siYtDw423
And...
I made a tweeked version to try to go closer to what I think you want :)
The code is here:
https://editor.p5js.org/v-k-/sketches/oMsWC2NHv
Perhaps you can play with the numbers to get What you need. For instance:
You can set the number of sides, or even reject sides smaller than something.
It's very simple stuff ;) Easy to tweek.
Have fun, hope the rats like it.
I want to use one shape (for instance a rectangle) to act as a mask or clipping path for another shape (for instance a circle, or line) in P5.js
I can see solutions for using images as masks, but not shapes. It seems mask() is not a function of shapes:
https://p5js.org/reference/#/p5.Image/mask
yes, you can.
create an extra rendering context with createGraphics().
In the draw loop draw something to this context which will be your
mask. Whatever should be visible in your result has to be colored
with the alpha channel, for example fill('rgba(0, 0, 0, 1)'.
Apply the mask to your original image myImage.mask(circleMask).
Your original image has now been modified by the mask, render it on
the screen: image(myImage, x, y, w, h)
Here is a working code example:
let circleMask;
let myImage;
function setup() {
createCanvas(400, 400);
circleMask = createGraphics(128, 128);
myImage = loadImage('FzFH41IucIY.jpg');
}
function draw() {
background(255);
circleMask.fill('rgba(0, 0, 0, 1)');
circleMask.circle(64, 64, 128);
myImage.mask(circleMask);
image(myImage, 200 - 64, 200 - 64, 128, 128);
}
There isn't a way to do this out of the box with P5.js.
Right now your question is more of a math question than it is a P5.js question. I'd recommend searching for something like "circle rectangle intersection" for a ton of results, including this one: Circle-Rectangle collision detection (intersection)
Depending on what you want to do, you could get away with drawing the shapes to images and then using those images as a mask. But more likely you're going to have to calculate the intersection yourself. You might be able to find a library that does this for you, but again, there isn't a simple out of the box way with P5.js.
I'm trying to draw an icon(.png) inside a QWidget with QPainter::drawPixmap()
:
QPixmap _source = "/.../.png";
painter.setRenderHint(QPainter::HighQualityAntialiasing);
painter.drawPixmap(rect(), _source);
but in comparing to QLabel (for example) and in lower size (19*19 in my case) the result isn't perfect.
What can I do?
****Edit****
QLabel with pixmap # size 19*19:
My painting # size 19*19 via SmoothPixmapTransform render type:
You are setting the wrong render hint, you need QPainter::SmoothPixmapTransform to get smooth resizing. By default the nearest neighbor method is used, which is fast but has very low quality and pixelates the result.
QPainter::HighQualityAntialiasing is for when drawing lines and filling paths and such, i.e. when rasterizing geometry, it has no effect on drawing raster graphics.
EDIT: It seems there is only so much SmoothPixmapTransform can do, and when the end result is so tiny, it isn't much:
QPainter p(this);
QPixmap img("e://img.png");
p.drawPixmap(QRect(50, 0, 50, 50), img);
p.setRenderHint(QPainter::SmoothPixmapTransform);
p.drawPixmap(QRect(0, 0, 50, 50), img);
img = img.scaled(50, 50, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
p.drawPixmap(100, 0, img);
This code produces the following result:
There is barely any difference between the second and third image, manually scaling the source image to the desired dimensions and drawing it produces the best result. This is certainly not right, it is expected from SmoothTransformation to produce the same result, but for some reason its scaling is inferior to the scale() method of QPixmap.
I am a little perplexed as to the trouble I am having with glOrtho(). My code works in the following case:
// Scenario 1
...
glOrtho(0, _width, 0, _height, 0, 1);
...
glRasterPos2i(0, 0);
...
However, I am unhappy with this coordinate system, I would like it to use:
// Scenario 2
...
glOrtho(0, _width, -_height, 0, 0, 1);
...
glRasterPos2i(0, -_height);
...
Unfortunately, only changing the two lines above in my code leaves me with a blank screen. I assumed I did not understand how glOrtho() and glRasterPos2i() work, as I am fairly new to OpenGl, so I tried the following:
// Scenario 3
...
glOrtho(0, _width, -_height, 1, 0, 1);
...
glRasterPos2i(0, -_height);
...
And, to my surprise, it worked! Why is this? The above code is not sufficient for my purposes, so I will stick with scenario 1 unless I can solve my problem. Does anyone have any insight as to why Scenario 1 and 3 work, but Scenario 2 does not? According to my web searches, Scenario 2 should work, so I must misunderstand something.
If it helps, I am using glDrawPixels() to draw an image, where _height and _width are the height and width of the image.
I am using Windows 7 and Qt 4.7.4.
If you need any more info, let me know.
edit: Typo in the original, it should read glOrtho(0, _width, -_height, 0, 0, 1) instead of glOrtho(0, _width, _height, 0, 0, 1).
You raster position is on the corner of the window. My guess is that round-off error is causing the position to be clipped in some cases but not others.
I think that the answer is glWindowPos(), which was added in version 1.4. Like the name says, it sets the raster position directly in window coordinates, unaffected by the modelview and projection matrices. Importantly, the raster position will always be valid, even if the position is on or outside of the window bounds. The disadvantage is that you'll have to do the coordinate mapping yourself before calling glWindowPos().
I am trying to create a "radar-like" control within our Flex application.
See the following link for an example (sorry, small image...)
http://www.radware.com/uploadedImages/Products/Management/Insite/Security-Dashboard.jpg
This is my first foray into drawing with Flex.
I would like to draw the main circles of the radar, the radar scanning shape, and circles/balls representing objects within the radar. These objects need to have the ability to be clicked on or hovered over so the user can get more information.
I'm looking at Sprite's and Shapes, etc. but confused as which to use to create such a control.
Any tips, links or suggestions would greatly be appreciated.
Thanks!
Couple of good options in Flex 3
http://www.axiis.org/
http://www.degrafa.org/blog/category/data-visualization/
http://visunetdemos.demos.ibm.com/webdemos/radar/radar.html
Before I jump into degrafa, I simply did the following to get the basic radar display drawn. Any thoughts or feedback on how I did this would be great.
var mySprite:Sprite = new Sprite();
var mySprite2:Sprite = new Sprite();
var mySprite3:Sprite = new Sprite();
mySprite3.graphics.beginFill(0x000000);
mySprite3.graphics.drawCircle(210, 210, 200);
mySprite2.graphics.lineStyle(2, 0xA10303, .75);
mySprite2.graphics.drawCircle(210, 210, 150);
mySprite.graphics.lineStyle(2, 0xA10303, .75);
mySprite.graphics.drawCircle(210, 210, 75);
this.rawChildren.addChild(mySprite3);
this.rawChildren.addChild(mySprite2);
this.rawChildren.addChild(mySprite);