How to crop image in a circle shape in Titanium? - image

You can crop an image by setting it to the background of a label and then setting the label to the size you want, but is there a way to crop it in a circle shape?
Thanks!

Setting borderRadius to half of the images width/height will give you a circle shaped image.
var imageView = Ti.UI.createImageView({
image : '/images/myImage.png',
width : 60,
height : 60,
borderRadius : 30
});
win.add(imageView);
For cropping images, imageAsCropped is also quite handy.

Very simple solution that use titanium ui. Without corners to be pixelated (Android).
<View id="userFaceContainer">
<ImageView id="avatar" />
<View id="userFaceInnerBorder" />
</View>
and styles
"#avatar": {
width: 90,
height: 90
}
"#userFaceContainer": {
width: 90,
height: 90,
center: {
x:"50%",
y:"50%"
},
borderRadius: 45,
borderColor: "#669dd3",
borderWidth:2
}
"#userFaceInnerBorder": {
width: 88,
height: 88,
borderRadius: 44,
borderColor: "#669dd3",
borderWidth:2,
opacity: 0.7
}

Related

How to center crop Images in Fabricjs?

I'm able to add Images to the canvas using Fabric.js.
However, I have a fixed width & height for the image box.
The images will be added dynamically and their width and height would vary.
To make it consistent, I want to center crop the Image to fit the width and height of the image box.
How do I do that using Fabric.js?
Sample Images
Here is my code:
let newObj = {
"version": "4.6.0",
"objects": [{
type: 'image',
version: '4.6.0',
originX: 'left',
originY: 'top',
left: 24,
top: 24,
width: 250,
height: 250,
fill: 'rgb(0,0,0)',
scaleX: 0.49,
scaleY: 0.49,
visible: true,
backgroundColor: '',
fillRule: 'nonzero',
paintFirst: 'fill',
globalCompositeOperation: 'source-over',
src: 'https://logo.clearbit.com/mircrosoft.com?size=400',
crossOrigin: null,
filters: [],
scaleToWidth: 250,
scaleToHeight: 250
}],
"background": "#000000"
}
var canvas1 = new fabric.StaticCanvas("a", { width: 600, height: 400 });
canvas1.loadFromJSON(newObj, function() {
canvas1.renderAll();
});
<script type='text/javascript' src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/521/fabric.min.js"></script>
<canvas id="a"></canvas>
I would recommend you that instead of cropping you should scale the original to fit the size you need, that way it works for big and small images, and it's a simple calculation.
In your sample you have hardcoded scaleX: 0.49 not sure why that, If the original image is quite large resolution that scale won't do much and center crop will get just a fraction of the image.
On my example below I'm assuming that your image box is a square, but if not you can use:
http://fabricjs.com/docs/fabric.Image.html#scaleX
http://fabricjs.com/docs/fabric.Image.html#scaleY
const canvas1 = new fabric.StaticCanvas("a", {width: 300, height: 200});
const image_url = 'https://logo.clearbit.com/mircrosoft.com?size=400'
new fabric.Image.fromURL(image_url, img => {
const size = 100
img.top = img.left = 20
img.scale(size/img.width);
canvas1.add(img);
canvas1.renderAll();
});
canvas {
border: solid 1px red
}
<script type='text/javascript' src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/521/fabric.min.js"></script>
<canvas id="a"></canvas>

React Native - Can't clickTouchableOpacity inside ImageBackground

I want create a rotate icon inside a image:
<ImageBackground style={stylesNative2.image} source={{ uri }} >
<TouchableOpacity onPress={ () => { alert("handler here") }} tyle={styles.rotateImageIcon}>
<Icon name='rotate-ccw' type='Feather' style={styles.rotateImageIcon} />
</TouchableOpacity>
</ImageBackground>
const stylesNative2 = StyleSheet.create({
image: {
zIndex: 0,
position: 'absolute',
height: h,
width: WIDTH,
resizeMode: 'cover',
transform: [{ rotate: this.state.imageRotation + 'deg' }]
}
});
const styles = StylesManager.getStyles({
rotateImageButton: {
backgroundColor: 'transparent',
elevation: 0,
zIndex: 1
},
rotateImageIcon: {
marginTop: '1rem',
marginLeft: '1rem',
fontSize: '1.7rem',
color: 'white',
}
});
The icon appear but the TouchableOpacity is not working.
Any idea why it's not working?
I think it's because ImageBackgroud have a pointerEvent like this one:
pointer-events: none;
http://facebook.github.io/react-native/docs/view#pointerevents
Can you try to wrap your <TouchableOpacity> on a <View>?
Mayby it's the zIndex...
A touchable opacity is transparent so you don't have to add zIndex on it.
When we add TouchableOpacity inside the ImageBackground .
We need to add the zindex greater than ImageBackground.
like zindex:100
Elements with a higher index will be placed on top of elements with a lower index. Note: Z index only works on positioned elements ( position:absolute , position:relative , or position: fixed

facing problems with styling on react native and achieving a better responsive layout

I tried to run my code on various screen sized emulators and the layout of my design varies from device to device. I need help to make the layout more responsive and fixed on different sized screens such as tablets, 3"mobile and 6"mobile.
-----------or connect with us on----------- I want to style this line in better way and that remains the same look on every device
I tried using the dimensions of the screen by defining the width and height using variables, and by describing the values in percentages, but nothing worked.
import React, { Component } from 'react';
import { View,TouchableOpacity, Text, StyleSheet, Image, Dimensions } from 'react-native';
import BacgroundImage from './BackgroundImage';
import Buttons from './Reusable/Button';
const { width: WIDTH } = Dimensions.get('window');
class LaunchScreen extends Component {
render() {
return (
<BacgroundImage>
<View style={styles.logoContainer}>
<Image
source={require('./images/logo.png')}
style={styles.PlaceLogo}
/>
</View>
<View style={styles.Buttons}>
<Buttons style={styles.signupButton}
onPress={() => navigate('Login')}>
<Text style={styles.buttonText}> SIGN UP</Text>
</Buttons>
<Buttons style={styles.loginButton}>
<Text style={styles.buttonText}> SIGN IN</Text>
</Buttons>
</View >
<View style={styles.sepratorView}>
<Text style={styles.sepratorText}>--------- or connect with us on ---------</Text>
</View >
<View style={styles.socialButtonStyle}>
<TouchableOpacity
style={styles.fbstyle}
activeOpacity={0.5}
>
<Image
source={require('./images/facebookicon.png')}
style={styles.iconstyle}
/>
<View
style={styles.sepratorLine}
/>
<Text
style={styles.socialButtonText}
>FACEBOOK</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.googlestyle}
onPress={this.signIn}
activeOpacity={0.5}
>
<Image
source={require('./images/google.png')}
style={styles.iconstyle}
/>
<View
style={styles.sepratorLine}
/>
<Text style={styles.socialButtonText} >GOOGLE</Text>
</TouchableOpacity>
</View>
</BacgroundImage>
);
};
}
const styles = StyleSheet.create({
logoContainer: {
flex: 1
},
PlaceLogo: {
width: WIDTH - 140,
margin: 75,
resizeMode: 'center',
justifyContent: 'center',
alignItems: 'center',
},
yosoButtons: {
width: WIDTH - 80,
justifyContent:'center',
marginTop:350
},
signupButton: {
height:40,
paddingTop:7,
marginBottom: 15,
},
loginButton: {
height:40,
paddingTop:7,
marginBottom: 15
},
buttonText: {
},
sepratorText: {
textAlign:'center',
fontSize:20,
color: '#b6b7ba',
},
sepratorView: {
flexDirection: 'row',
justifyContent:'center',
alignItems:'center'
},
socialButtonStyle:{
flex:1,
flexDirection:'row',
justifyContent:'center',
alignItems:'center',
margin:5
},
fbstyle:{
flex:1,
flexDirection:'row',
borderColor:'white',
alignItems:'center',
backgroundColor:'#485a96',
borderWidth:1,
borderRadius:35,
height: 40,
marginLeft:15,
marginRight:15
},
googlestyle:{
flex:1,
flexDirection:'row',
alignItems:'center',
backgroundColor:'#dc4e41',
borderWidth:1,
borderRadius:35,
borderColor:'white',
height: 40,
marginRight:15
},
iconstyle:{
resizeMode:'stretch',
height:25,
width:25
},
sepratorLine:{
backgroundColor:'white',
width:2,
height: 40
},
socialButtonText:{
color: '#fff',
flex:1,
textAlign: 'center',
fontSize: 15,
fontWeight: "bold"
}
});
export default LaunchScreen;
I want the layout to remain the same. Both the logo and buttons remain at the same position on every device display.
And I want a better dynamic solution for separator I have used in code
--------------or connect with us on-----------
I got a workaround solution for this problem. I am assigning values using the height and width of the screen to the styling.
import { StyleSheet, Dimensions } from 'react-native';
const { width: WIDTH, height: HEIGHT } = Dimensions.get('window');
SomeText: {
fontSize: HEIGHT / 38,
marginTop: WIDTH / 20,
}

React-Native ImageEditor.cropImage shows no picture

I've been trying to cut a picture into several parts for hours now.
I thought it would work with ImageEditor.cropImage. Apparently I'm using the function wrong, or it doesn't do what I expect it to do.
Can someone please tell me what I'm doing wrong, or recommend a package I can use. Thank you.
My attempt:
const cropData = {
offset: {x: 25, y: 25},
size: {width: 250, height: 250},
displaySize: {width: 250, height: 250},
resizeMode: 'contain',
};
later..
ImageEditor.cropImage(require('../resources/images/pictures/categoryOne/c1b1.jpg'),
cropData);
I solved my issue not with ImageEditor.cropImage but with CSS
<View style={styles.topLeftView}>
<Image
style={styles.topLeftImage}
source={require('../resources/images/pictures/categoryOne/c1b1.jpg')}
resizeMode="contain"
/>
</View>
Style
topLeftView: {
width: 100,
height: 100,
overflow: 'hidden',
},
topLeftImage: {
right: 0,
bottom: 0,
},

Why is this error in Image Component of React-Native?

I would like to background black opacity.
I've wrote this code.
<Image source={require('../assets/images/test_img1.png')} style={[s.lastReviewImage]}>
<View style={s.blackOverlay}/>
</Image>
lastReviewImage: {
flex:1,
width: null, height: null
},
blackOverlay: {
position: 'absolute',
top: 0,
right: 0,
bottom: 0,
left: 0,
backgroundColor: 'red',
opacity: 0.3
},
But, this is give me a error.
Error: The Image component cannot contain children. if you want to render content on top of the image, consider using absolute positioning.
I know what this error means but I've surely set absolute property in blackOverlay.
I've refer this guides.
Text Overlay Image with Darkened Opacity React Native
https://snack.expo.io/S15Lt3vjg
But, It's not working for me with the error.
If you want to set some background color or an image as the Screen Background, you need to use ImageBackground in place of Image.
<ImageBackground
source={yourSourceFile}
style={s.blackOverlay}>
<....yourContent...>
</ImageBackground>

Resources