Titanium ImageView can't scale the size by setting the height and width properties? - imageview

I'm trying to display an image as the background of my app, and I try to use an ImageView, the code blew:
bg-img = TUI.create-image-view({
image: ev.media
left: 0
top: 0
border-width: 2
border-color: "red"
# width: img-width + "px"
# height: img-height + "px"
})
bg-img.width = 2754
bg-img.height = 4896
# pic-img.add bg-img
bg-img.add-event-listener \click, !(ev)->
log \clicked-on-pic-img
_this.trigger \click-edit-pic-view, ev
# # inspect pic-img
_this.Els.pic-container.add bg-img
The image doesn't apear in the screen, but If I minus the size of bg-img like:
bg-img = TUI.create-image-view({
image: ev.media
left: 0
top: 0
border-width: 2
border-color: "red"
# width: img-width + "px"
# height: img-height + "px"
})
bg-img.width = 2754 / 3
bg-img.height = 4896 / 3
# pic-img.add bg-img
bg-img.add-event-listener \click, !(ev)->
log \clicked-on-pic-img
_this.trigger \click-edit-pic-view, ev
# # inspect pic-img
_this.Els.pic-container.add bg-img
It can't run correctly, I don't why? o(╯□╰)o
some one can help? Thx!

my origin code is blew, And my purpose is to dynamic get a image from photogallary, and set the img as a backgroundimage of a View whose size is decided by the image:
(and I use the https://github.com/6174/TiDraggable module)
create-pic-view.bind \photo-gallery, ->
_this = #
log \open-photo-gallery
Ti.Media.open-photo-gallery({
success: !(ev)->
log \success-get-image-event
# inspect ev
#image size in pixels
img-width = ev.width
img-height = ev.height
# convertPointToView( Point point, Titanium.UI.View destinationView ) : Point
# Translates a point from this view's coordinate system to another view's coordinate system.
pic-img = _this.Els.pic-img = draggable.create-view({
background-image: ev.media.native-path
top: 0
left: 0
width: img-width
height: img-height
})
pic-img.add-event-listener \click, !(ev)->
log \clicked-on-pic-img
_this.trigger \click-edit-pic-view, ev
_this.Els.pic-container.add pic-img
})

Related

GameMaker: 3D floor collision issue

So I'm trying to create 3D collisions for a floor object, and when I jump in-between 2 floor objects, with one being a higher Z axis than the other, as soon as my vertical speed goes even slightly down, I get teleported to be on the floor.
Variable List:
z - Z axis
zspeed - Vertical (Z) speed
grounded - On the ground or not.
Event: Collision with obj_floor
Code:
if z = clamp(z,other.z-5,other.z+1) && zspeed < 0 {
z = other.z;
grounded = true;
}
else { grounded = false; }
if z = other.z {
zspeed = 0;
grounded = true;
}
if z = clamp(z,-5,other.z-2) {
x=xprevious;
y=yprevious;
}
Is there a solution for this? That'd be much appreciated, thank you.
Try something more like this:
if (zspeed > 0 and z > other.z-5 and z < other.z+1) {
z = other.z;
grounded = true;
zspeed = 0;
} else {
grounded = false;
}
Of course there will be other situations outside of this routine where you should should set grounded to false, probably when there is no collision at all.
Basically if the player collides with the platform and is within a certain range of z and is moving downwards (zspeed > 0), set it to z. Otherwise do nothing.
I have never used zspeed, so I assume zspeed of > 0 is moving "downwards".
As for clamp I suspect you have misunderstood what it does. Your use of it suggests you think that it returns true if z is within the min and max. What it actually does is provide you a value of z such that is is within min and max.
So if your value of z is 100000, clamp(z, -5, 5) will return 5. I don't think this is what you want :)
See link:
https://manual.yoyogames.com/GameMaker_Language/GML_Reference/Maths_And_Numbers/Number_Functions/clamp.htm

Error of 'argument is of length zero' in R

I have a 18x18 matrice of 1s and 0s. However I have this below code that adds padding to the matrix to make it 20x20 and then check the neighbours of each element of a matrice if any matrice has all of its neighbours combined just as 1.
counter = 0
imageNewMatrix <- imageMatrix
nrow(imageMatrix)
ncol(imageMatrix)
imageNewMatrixa <- cbind(imageNewMatrix, 0)
imageNewMatrixB <- rbind(imageNewMatrixa, 0)
imageNewMatrixC <- cbind(imageNewMatrixB, 0)
imageNewMatrixD <- rbind(imageNewMatrixC, 0)
nrow(imageNewMatrixD)
ncol(imageNewMatrixD)
for (row in 1:nrow(imageNewMatrixD)) {
for (col in 1:ncol(imageNewMatrixD)) {
if (imageNewMatrixD[row,col] == 1) {
# Get entry of the left pixel
pixel_to_left = as.numeric(imageNewMatrixD[row, col-1])
# Get entry of the right pixel
pixel_to_right = as.numeric(imageNewMatrixD[row, col+1])
# Get entry of the pixel at top
pixel_at_top = as.numeric(imageNewMatrixD[row-1, col])
# Get entry of the pixel at top-left
pixel_at_top_left = as.numeric(imageNewMatrixD[row-1, col-1])
# Get entry of the pixel at top right
pixel_at_top_right = as.numeric(imageNewMatrixD[row-1, col+1])
# Get entry of the pixel at bottom
pixel_at_bottom = as.numeric(imageNewMatrixD[row+1, col])
# Get entry of the pixel at bottom left
pixel_at_bottom_left = as.numeric(imageNewMatrixD[row+1, col-1])
# Get entry of the pixel at bottom right
pixel_at_bottom_right = as.numeric(imageNewMatrixD[row+1, col+1])
pixel_sum = pixel_to_left + pixel_to_right + pixel_at_top +
pixel_at_top_left + pixel_at_top_right + pixel_at_bottom +
pixel_at_bottom_left + pixel_at_bottom_right
if (as.numeric(pixel_sum == 0)) {
counter = counter + 1
} else {
counter = 0
}
}
}
}
neigh_1 = counter
Whenever I try to run this code, the error Error in if (as.numeric(pixel_sum == 0)) { : argument is of length zero shows up. Can someone help me with this error?
I made a mistake of seeking for columns from n to nrow. Instead I used 2:19 cols and 2:19 rows.
I also used isTRUE when checking if pixel sum is equal to 0 or not.

RMagick convert image with alpha

I'm trying to convert an image to 25% opacity via RMagick. The following works from the command line but I can't figure out how to do it in Ruby.
convert input.png -alpha on -channel A -evaluate set 25% +channel output.png
I've tried messing with Magick::AlphaChannelType::ActivateAlphaChannel but can't figure out how to chain the commands together. Any help would be greatly appreciated!
plz try this function to solve your problem
def watermark(opacity = 0.25, size = 'input')
manipulate! do |img|
logo = Magick::Image.read("#{Rails.root}/app/assets/images/{size}.png").first
logo.alpha(Magick::ActivateAlphaChannel)
white_canvas = Magick::Image.new(logo.columns, logo.rows) { self.background_color = "none" }
white_canvas.alpha(Magick::ActivateAlphaChannel)
white_canvas.opacity = Magick::QuantumRange - (Magick::QuantumRange * opacity)
# Important: DstIn composite operation (white canvas + watermark)
logo_opacity = logo.composite(white_canvas, Magick::NorthWestGravity, 0, 0, Magick::DstInCompositeOp)
logo_opacity.alpha(Magick::ActivateAlphaChannel)
# Important: Over composite operation (original image + white canvas watermarked)
img = logo.composite(logo_opacity, Magick::NorthWestGravity, 0, 0, Magick::OverCompositeOp)
end
end

OL5 VectorTileLayer: Y coordinate of tile

I try to use OL5 vector tiles with global tile grid.
import 'ol/ol.css'
import { Map, View } from 'ol'
import MVT from 'ol/format/MVT'
import TileGrid from 'ol/tilegrid/TileGrid'
import VectorTileLayer from 'ol/layer/VectorTile'
import VectorTileSource from 'ol/source/VectorTile'
let zoom = 0
let center = [8531000, 5342500]
let resolutions = [
9.554628535647032,
4.777314267823516,
2.388657133911758,
1.19432856695588,
]
let extent = [0, 0, 20037508.342789244, 20037508.342789244]
const map = new Map({
target: 'map',
view: new View({
zoom: zoom,
center: center,
resolutions: resolutions,
})
})
const vectorTiles = new VectorTileLayer({
source: new VectorTileSource({
tileSize: 256,
projection: 'EPSG:3857',
format: new MVT(),
tileGrid: new TileGrid({
extent: extent,
resolutions: resolutions,
}),
url: 'http://localhost:8000/get-vector-tiles/{z}/{x}/{y}'
})
})
map.addLayer(vectorTiles)
A request looks like http://localhost:8000/get-vector-tiles/0/3487/6007,
as I understand {x}/{y} are coordinates(numbers) of a tile from origin(in my case 0,0).
The start resolution is 9.554628535647032,
therefore a tile size is 9.554628535647032 × 256 = 2445.984905126 meters
Estimating of requested area coordinates:
X: 2445.984905126 × 3487 = 8529149.3642
Y: 2445.984905126 × 6007 = 14693031.2943
Considering the center of map is [8531000, 5342500]:
X coordinate is right 8529149.3642 ~ 8531000,
while Y coordinate does not match 5342500 vs 14693031.2943
What's wrong?
Solved, the thing is the rendering starts at the top left corner, therefore 20037508.342789244 - 2445.984905126 × 6007 ~ 5342500, all right!

How to find x,y coordinates of a circle in Pygame

I am currently trying to create a digital speedometer. The project has come to a screeching halt due to the problem encountered when trying to control the dial. My variable radial_pos is intended to calculate the exact x,y coordinates of each tick of the speedometer and if the user presses K_UP, radial_pos will increase by 1 to show the next tick of the speedometer. However, I'm afraid its been too many years since I've learned the concept of finding the coordinates of a circle, let alone understand it enough to be able to increment it accordingly. Here is my code so far:
import pygame, sys
from pygame.locals import *
pygame.init()
#Screen Resolution
width = 1080
height = 720
#Instrument Characteristics
color = (255,255,255)
background = (0,0,0)
radius_speedometer = 100
weight_speedometer = radius_speedometer-5
#Window Measurements
screen= pygame.display.set_mode((width,height),0,32)
pos_center_x = width/2
pos_center_y = height/2
#Speedometer Dial Position
pos_speedometer_dial_x = pos_center_x - 60
pos_speedometer_dial_y = pos_center_y +60
#Radial Position (hint: ticks on the clock)
'''
radial_pos = ...
'''
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type==KEYDOWN:
if event.key==K_DOWN:
radial_pos=-1
if event.key==K_UP:
radial_pos=+1
if event.type==KEYUP:
if event.key==K_DOWN:
radial_pos=0
if event.key==K_UP:
radial_pos=0
screen.lock()
#Speedometer Ring
pygame.draw.circle(screen, color, (pos_center_x,pos_center_y), radius_speedometer)
pygame.draw.circle(screen, background, (pos_center_x,pos_center_y), weight_speedometer)
#Speedometer Dial
pygame.draw.line(screen, color, (pos_center_x,pos_center_y), (pos_speedometer_dial_x, pos_speedometer_dial_y),5)
screen.unlock()
pygame.display.update()
If anyone has any advice for this particular issue, it would greatly appreciated. Thank you for your time.
It seems I have finally found a solution that works well. Here is the most updated code:
import pygame, sys
from pygame.locals import *
from math import *
pygame.init()
#Screen Resolution
width = 1080
height = 720
#Instrument Characteristics
color = (255,255,255)
background = (0,0,0)
radius_speedometer = 100
weight_speedometer = radius_speedometer-5
#Window Measurements
screen= pygame.display.set_mode((width,height),0,32)
pos_center_x = width/2
pos_center_y = height/2
#Speedometer Dial Position
pos_speedometer_dial_x = pos_center_x - 60
pos_speedometer_dial_y = pos_center_y +60
#Radial Position (hint: ticks on the clock)
radial_pos = 70
move_rad = 0
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type==KEYDOWN:
if event.key==K_DOWN:
move_rad=-0.01
if event.key==K_UP:
move_rad=+0.01
if event.type==KEYUP:
if event.key==K_DOWN:
move_rad=-0.05
if event.key==K_UP:
move_rad=-0.005
angle = 2.0*pi*radial_pos/120.0
radial_pos+=move_rad
pos_speedometer_dial_x = pos_center_x + (radius_speedometer-10)*sin(angle)
pos_speedometer_dial_y = pos_center_y - (radius_speedometer-10)*cos(angle)
screen.lock()
#Speedometer Ring
pygame.draw.circle(screen, color, (pos_center_x,pos_center_y), radius_speedometer)
pygame.draw.circle(screen, background, (pos_center_x,pos_center_y), weight_speedometer)
#Speedometer Dial
pygame.draw.line(screen, color, (pos_center_x,pos_center_y), (pos_speedometer_dial_x, pos_speedometer_dial_y),5)
screen.unlock()
pygame.display.update()
It's not perfect (such as no zero yet), however we are now much closer to completing the first instrument of the dash cluster!

Resources