What is the correct way to parent ImageBundles in the Bevy game engine so the child image doesn't alter the size of the parent image? - bevy

When I try to parent two ImageBundles together I don't get the expected result. The child image causes the parent image to resize to the size of the child image. I am trying to place the button image on top of the background image, but the following code places the button image on the background image and then scales the background image to the size of the button image.
If I comment out the push_children line of code then both images are displayed at the correct size.
let ent1 = commands.spawn_bundle(ImageBundle {
style: Style {
align_self: AlignSelf::Center,
..Default::default()
},
material: materials.add(
asset_server.load("Background.png").into()),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
..Default::default()
}).id();
let ent2 = commands.spawn_bundle(ImageBundle {
style: Style {
align_self: AlignSelf::Center,
..Default::default()
},
material: materials.add(
asset_server.load("button.png").into()),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
..Default::default()
}).id();
commands.entity(ent1).push_children(&[ent2]);

Related

Changing world size in flame with parallax

I'm using the version 1 release of Flame, and adding a parallax background which works great.
// load background layers
final layers = _layersMeta.entries.map(
(e) => loadParallaxLayer(
ParallaxImageData(e.key),
velocityMultiplier: Vector2(e.value.toDouble(), 1.0),
alignment: Alignment.center,
fill: LayerFill.none ,
),
);
// create background parallax
parallax = ParallaxComponent(
parallax: Parallax(
await Future.wait(layers),
baseVelocity: Vector2(20, 0),
),
);
add(parallax);
Once I set the camera to follow a Sprite camera.followVector2(player.position); then its obvious that the parallax was initialized to the size of the screen because as the camera moves, the edges of the parallax become visible with a black game world background. I've tried changing the size of the ParallaxComponent and the Parallax to no avail.
Changing the canvas size doesn't do much either onGameResize(Vector2(camera.canvasSize.x, 5000));
Is there a standard way to set a component fill the world size? The source code talks about the world size, but I don't see any way to control it.
The ParallaxComponent is a PositionComponent which lives in the world that the camera is viewing by default. If you want a PositionComponent to be relative to the viewport (or the GameWidget) you have to change its positionType to PositionType.viewport.
So in this case the initialization of your ParallaxComponent could look like this to make it ignore the camera:
// create background parallax
parallax = ParallaxComponent(
parallax: Parallax(
await Future.wait(layers),
baseVelocity: Vector2(20, 0),
),
)..positionType = PositionType.viewport;

Showing a fix boundary box to capture image

My ionic appp is to take pictures of business cards. as 99% of the business cards are landscape style so users tries to change the camera orientation to landscape mode as well. This is a natural behavior.
However, i want to avoid that and one way is to show a rectangle while camera is open (width equal to screen width and a 3:2 aspect ratio for height)
This will make life easy as users wont try to change the camera orientation.
I was looking into camera plugin which uses code like
this.camera.getPicture({
destinationType: this.camera.DestinationType.DATA_URL,
quality: 25,
correctOrientation: true,
allowEdit:false,
sourceType: this.camera.PictureSourceType.SAVEDPHOTOALBUM
}).then(async(imageData) => {
//console.log("image data is:" + imageData)
// imageData is a base64 encoded string
var base64Image = "data:image/jpeg;base64," + imageData;
I was trying targetWidth and height but that does not draw that box like i have seen in many other apps.
there are other plugins like cropperJs but seems they let u crop the image after taken which not what i need.
Use camera-preview-plugin instead of camera for that :
const cameraPreviewOpts: CameraPreviewOptions = {
x: 0,
y: 0,
width: window.screen.width,
height: window.screen.height,
camera: 'rear',
tapPhoto: true,
previewDrag: true,
toBack: true,
alpha: 1
}
Ionic Camera Preview

Javafx scale a control according to it's parent

I am writing a new Imageview with mask in Javafx.
The mask can show according to the direction of mouse.
Using Timeline makes it work well. But when the mask leaves, the mask is still visible.
I want to hide the part of mask, which is out of the Image. How can I scale it?
When mouse enter:
When mouse leave:
And my animation code
private void leftOut() {
KeyFrame k1 = new KeyFrame(Duration.ZERO, new KeyValue(mask.translateXProperty(), 0));
KeyFrame k2 = new KeyFrame(time, new KeyValue(mask.translateXProperty(), control.getWidth()*-1.0));
timeline = new Timeline();
timeline.getKeyFrames().addAll(k1,k2);
timeline.play();
}
Other directions are just like this.
You don't need to resize the mask at all. Simply apply a clip to it that has the same size as the image:
Image image = new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/b/b1/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF.jpg/164px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF.jpg");
ImageView imageView = new ImageView(image);
// init mask
Label mask = new Label("SomeText");
mask.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
mask.setPrefSize(image.getWidth(), image.getHeight());
mask.setStyle("-fx-background-color: rgba(255, 255, 255, 0.6);");
mask.setAlignment(Pos.CENTER);
// create clip
Rectangle clip = new Rectangle(image.getWidth(), image.getHeight());
mask.setClip(clip);
// keep clip in position horizontally in root
clip.translateXProperty().bind(mask.translateXProperty().negate());
StackPane root = new StackPane(imageView, mask);
root.setPrefSize(500, 500);
root.setStyle("-fx-background-color: orange;");
// probably a bit simpler to use TranslateTransition instead of Timeline
TranslateTransition transition = new TranslateTransition(Duration.seconds(5), mask);
transition.setByX(image.getWidth());
transition.play();

How can i show the image within the circle using qml?

Hi I'm new to blackberry. I want to show the image within the circle using qul.
The easiest way to vignette an image is to overlay it with another image. In Cascades if you create a Container using either DockLayout or AbsoluteLayout you can position multiple ImageView objects on top of one another. The first object will be on the bottom, last on the top, by default. You simply create an image with a transparent viewport of the shape you want. You can scale the image at runtime but you should make it about the size of the largest frame you will need to preserve quality. I used Gimp to create a 786 x 78x pixel image with a white frame and a transparent circle in the middle and saved it to the assets directory as frame.png. I grabbed another image I had easily to hand and create a BlackBerry project with this QML code in an appropriate place:
Page {
Container {
layout: DockLayout {
}
ImageView {
imageSource: "asset:///FusionMap.PNG"
preferredHeight: 360
preferredWidth: 360
}
ImageView {
imageSource: "asset:///frame.png"
preferredHeight: 360
preferredWidth: 360
}
}
}
This is the result:

Raphael-GWT: fill image of a shape (Rect) appears offset. How to resolve this?

I'm wondering about the behavior of {Shape}.attr("fill","url({image.path})").
when applying a fill image to a shape:
public class AppMapCanvas extends Raphael {
public AppMapCanvas(int width, int height) {
super(width, height);
this.hCenter = width / 2;
this.vCenter = height / 2;
...
Rect rect = this.new Rect(hCenter, vCenter, 144, 40, 4);
rect.attr("fill", "url('../images/app-module-1-bg.png')"); // <--
...
}
}
The background image seem to teal accross the canvas behind the shape, thus gets weird positioning (an illustration snapshot is enclosed - i marked the original image borders in red).
This seem to resolve itself in the presence of an animation along a path (a mere path.M(0,0) is sufficiant).
How can i position the fill-image properly in the first place?
The proper way to do this from what I can understand would be to use an SVG pattern declaration to specify the portion and position of the image you would want to use. Then you would use that pattern to fill the rectangle element. Unfortunately, the Raphael javascript library doesn't have support for patterns... so there's no direct way to use an image to fill a rectangle.