On p5JS canvas changing background image showing already drawn objects behind the background image - p5.js

I'm drawing some objects on canvas but if later I change the background image on demand, then already drawn objects are behind the background image. How to bring already drawn objects in front. Below is the sample code i'm using to change the background image.
function draw() { if(chnBg){ //if change background is clicked loadImage("images/Grid_Image.png",function(bg){
background(bg);
}); } }

You will need to set globalCompositeOperation to destination-over before drawing / changing the background image ...
let canvas;
// setup
function setup() {
canvas = createCanvas(width, height);
}
// draw
function draw() {
if (chnBg) { //if change background is clicked
loadImage("images/Grid_Image.png", function(bg) {
canvas.drawingContext.globalCompositeOperation = 'destination-over'; //<-- set this
background(bg);
});
}
}

You could just store your state in a data structure of some kind and redraw everything each frame.
Another approach would be to draw your objects to a buffer graphics instead of directly to the canvas. Then draw the background to the canvas, and then draw the buffer to the canvas.
More info is available in the reference.

Related

In Flutter, how to associate device pixel ratio with a canvas created from scratch?

I'm using the following code snippet to record and get an image from a canvas. The image shows fuzzy on some devices.
setLayerBuffer(int layerID) async {
recorder = new ui.PictureRecorder();
canvas2 = new Canvas(recorder, canvasRect); //canvasRect is the drawing area
for (var myLayer in layers)
if (myLayer.id == layerID) {
if (myLayer.buffer != null) {
canvas2.drawImage(myLayer.buffer, new Offset(0.0, 0.0), Paint());
}
doPaint(canvas2, 1); //this will draw on canvas2
break;
}
var picture = recorder.endRecording();
buffer = await picture.toImage(
canvasRect!.width.round(), canvasRect!.height.round());
setBuffer(layerID, buffer!); // store image as buffer in layer
paintController.myNotifyListeners(); //paint the main canvas wiht buffers of layers
}
According to this https://github.com/flutter/flutter/issues/17782, the canvas created from scratch is not being set with device pixel ratio unlike the canvas created by CustomPaint.
I did try to set canvas scale with device pixel ratio as suggested by the above link. However, the image disappears after setting the scale. How do I set the canvas to use the device pixel ratio ?

zoom in one state in gicentre geomap on processing

I am using processing 3, and trying to implement an interactive map via gicentre GeoMap library. I have got the U.S. map shown and the hovering feature work i.e. highlight the hovering state. I am wondering is there any way I can zoom into the state in this GeoMap library. Maybe a mouseClick or a mouseMove event to trigger this function. I am not sure how to redraw the map to make it zoom into the selected state. Here is my starting code:
import org.gicentre.geomap.*;
GeoMap geoMap;
int id = -1;
void setup()
{
size(800, 400);
geoMap = new GeoMap(this); // Create the geoMap object.
geoMap.readFile("usContinental"); // Read shapefile.
}
void draw()
{
background(202, 226, 245); // Ocean colour
stroke(0,40); // Boundary colour
fill(206,173,146); // Land colour
//if (id == -1) {
geoMap.draw(); // Draw the entire map.
//} else {
// geoMap.draw(id);
//}
// Find the country at mouse position and draw in different color.
id = geoMap.getID(mouseX, mouseY);
if (id != -1)
{
fill(180, 120, 120); // Highlighted land colour.
geoMap.draw(id);
}
}
Any idea? Thanks!
Questions like these are best answered by looking at the docs for the library. Here are the docs for the giCentre geoMap library.
According to that, this library basically just shows shape files, without any fancy logic for zooming the map. You could implement this yourself using the scale() function or the camera functions. But you might be best off just finding a library that supports changing the zoom out of the box.

erase line in canvas with image background

i'm looking for a way to erase drawing lines in canvas without erasing my background image.
when i tried using white color i get white line on the image.
i thought about making transparent line but i don't think that's possible.
on saving the canvas with toDataURL() i want to save the drawing with the background.
this is how i set background image:
var background = new Image();
background.src = "pizza.png";
background.onload = function(){
context.drawImage(background,0,0);
}
You can "erase" with Compositing, but it's not recommended for lines
You can use compositing to "erase" a previously drawn line by redrawing it with globalCompositeOperation='destination-out'. This causes the previous line to become transparent pixels. Then you can reapply the background image over just the transparent pixels using globalCompositeOperation='destination-over'.
But there's a problem with lines. The problem with "erasing" lines is that canvas will automatically apply anti-aliasing to every line. That added anti-aliasing is difficult to remove.
The better (more typical) way of erasing canvas lines drawn over a background is to redraw everything:
Save all line definitions in an array (eg lines[]),
To remove line(s), remove their definitions from the lines[] array,
Erase the whole canvas,
Redraw the background,
Redraw all lines still in the lines[] array.
Here's annotated code and a demo:
// canvas vars
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;
// save line definitions
var lines=[
{x0:75,y0:50,x1:200,y1:100,color:'red'},
{x0:75,y0:75,x1:200,y1:100,color:'green'},
{x0:75,y0:100,x1:200,y1:100,color:'blue'},
{x0:65,y0:50,x1:65,y1:200,color:'green'},
]
// load the background
var img=new Image();
img.onload=start;
img.src="https://dl.dropboxusercontent.com/u/139992952/multple/bk.png";
function start(){
// draw the first time
draw();
// remove green lines on button click
$('#go').click(function(){ remove('green'); });
}
function draw(){
// clear canvas
ctx.clearRect(0,0,cw,ch);
// redraw background
ctx.drawImage(img,0,0);
// redraw all lines still in lines[]
ctx.lineWidth=3;
for(var i=0;i<lines.length;i++){
var l=lines[i];
ctx.beginPath();
ctx.moveTo(l.x0,l.y0);
ctx.lineTo(l.x1,l.y1);
ctx.strokeStyle=l.color;
ctx.stroke();
}
}
function remove(color){
for(var i=lines.length-1;i>=0;i--){
if(lines[i].color==color){lines.splice(i,1);}
}
draw();
}
body{ background-color: ivory; }
#canvas{border:1px solid red; margin:0 auto; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<button id=go>Remove green lines</button>
<br>
<canvas id="canvas" width=300 height=300></canvas>

HTML5 canvas, scale image after drawing it

I'm trying to scale an image that has already been draw into canvas.
This is the code:
var canvas = document.getElementById('splash-container');
var context = canvas.getContext('2d');
var imageObj = new Image();
imageObj.onload = function() {
// draw image at its original size
context.drawImage(imageObj, 0, 0);
};
imageObj.src = 'images/mine.jpeg';
// Now let's scale the image.
// something like...
imageObj.scale(0.3, 0.3);
How should I do?
You're thinking about it wrong. Once you've drawn the image onto the canvas it has no relationship to the imageObj object. Nothing you do to imageObj will affect what's already drawn. If you want to scale the image, do in the drawImage function:
drawImage(imgObj, 0, 0, imgObj.width * 0.3, imgObj.height * 0.3)
If you want to animate the scaling or are looking to achieve some other effect which requires you to draw the image at full size initially you'll have to first clear it before drawing the scaled down image.
What robertc says is correct, but if you really wanted to scale an image on a canvas after drawing it for some reason, you could just scale the whole canvas using the CSS width/height properties and that would scale the image without having to redraw it.

Drag an Image in XNA

I am working on an app in which images are flying on the Screen.
I need to implement:
Hold onto any of the flying images on Tap
Drag the image to certain position of the user's choice by letting the user hold it.
Here is another easy way to do dragging.
Just draw your image (Texture2d) with respect to a Rectangle instead of Vector2.
Your image variables should look like this
Texture2d image;
Rectangle imageRect;
Draw your image with respect to "imageRect" in Draw() method.
spriteBatch.Draw(image,imageRect,Color.White);
Now in Update() method handle your image with single touch input.
//Move your image with your logic
TouchCollection touchLocations = TouchPanel.GetState();
foreach(TouchLocation touchLocation in touchLocations)
{
Rectangle touchRect = new Rectangle
(touchLocation.Position.X,touchLocation.Position.Y,10,10);
if(touchLocation.State == TouchLocationState.Moved
&& imageRect.Intersects(touchRect))
{
imageRect.X = touchRect.X;
imageRect.Y = touchRect.Y;
}
//you can bring more beauty by bringing centre point
//of imageRect instead of initial point by adding width
//and height to X and Y respectively and divide it by 2
There's a drag-and-drag example in XNA here: http://geekswithblogs.net/mikebmcl/archive/2011/03/27/drag-and-drop-in-a-windows-xna-game.aspx
When you load your image in, you'll need a BoundingBox or Rectangle Object to control where it is.
So, in the XNA app on your phone, you should have a couple of objects declared for your texture.
Texture2D texture;
BoundingBox bBox;
Vector2 position;
bool selected;
Then after you load your image content, keep your bounding box updated with the position of your image.
bBox.Min = new Vector3(position, 1.0f);
bBox.Max = new Vector3(position.X + texture.Width, position.Y + texture.Height, 0f);
Then also in your update method, you should have a touch collection initialized to handle input from the screen, get the positions of the touch collection, loop through them and see if they intersect your boundingbox.
foreach (Vector2 pos in touchPositions)
{
BoundingBox bb = new BoundingBox();
bb.Min = new Vector3(pos, 1.0f);
bb.Max = new Vector3(pos, 0f);
if (bb.Intersects(bBox)
{
if (selected)
{
//do something
}
else
{
selected = true;
}
}
}
From there, you have whether your object is selected or not. Then just use the gestures events to determine what you want to do with your texture object.

Resources