I'm trying to create a text input on top of a fabric canvas using either IText or Textbox objects. This seems to be accomplished pretty well on http://impretty.me, which is using Fabric, but I can't seem to replicate what they're doing here. I'd either like to be able to set a max width/max character count on IText or limit the Textbox object to a single line.
My issue is that setting a width on IText is ineffective, and even though the width parameter works on Textbox, I need to keep the text to a single line. Scaling text, as is seen on http://impretty.me would be nice but isn't necessary. Any help would be greatly appreciated.
Code for the text box and the interaction I've worked out at this point is below.
const text = new fabric.IText('Type Your Message', {
originY: 'center',
hoverCursor: 'text',
left: 210,
top: 825,
width: 300,
fontFamily: 'Permanent Marker',
fontSize: 50,
hasBorders: false,
hasControls: false,
lockMovementX: true,
lockMovementY: true,
id: 'text'
})
canvas.add(text)
text.on('changed', function (options) {
if (this.width >= 635) {
const newText = this.text.slice(0, -1)
this.text = newText
this.hiddenTextarea.value = newText
console.log(this)
}
})
you can resize down the text to fit inside the bounding box
sizeDownTextNew = function(canvasObj, limit_height, limit_width )
{
if(typeof (canvasObj) == "undefined" || canvasObj == null)
return;
var counter = 250; //Maximum of 250iterations to size down
var initial_height = limit_height;
var max = canvasObj.get('fontSize');
var min = 6; //This is our minimum size
var foundmin=0; //Flag if minimum is met
var ratio = max;
//Now reduce the size of actual text font size property
while(canvasObj.height+2<canvasObj._getTextHeight()) //While the height of text is larger then actual text, try reducing the lineHeight
{
var fontSize = parseFloat(canvasObj.get('fontSize'));
fontSize=fontSize - fontSize/max; //reduce the size with a set ratio
if(fontSize > min) //If main font is greater then min font, we will now reduce
{
canvasObj.set('fontSize', fontSize);
canvasObj.height = initial_height
canvasObj.canvas.renderAll();
canvasObj.height = initial_height;
canvasObj.setCoords();
canvasObj.canvas.renderAll();
if(counter == 0) //Failsafe to exit the while loop if no condition is met
break;
}
counter = 300;
while(limit_width<canvasObj._getTextWidth()) //While the width of text is larger then actual text, try reducing it
{
var fontSize = parseFloat(canvasObj.get('fontSize'));
fontSize=fontSize - fontSize/ratio; //reduce the size with a set ratio
canvasObj.setCoords();
canvasObj.canvas.renderAll();
}
else
{
break;
}
canvasObj.width = limit_width;
counter -- ;
if(counter == 0)
{
break;
}
}
}
and then call this function like
text.on('changed', function (options) {
sizeDownTextNew(this,500,60);
})
you can add 'maxLength' to textarea.
this.on("editing:entered", () => {
if (this.hiddenTextarea) {
// limit textarea length
this.hiddenTextarea.maxLength = 40;
}
});
To limit text for fabric js Version 2+,
We need to override the onInput function as follows
onInput: function(e) {
if(this.width > this.maxWidth) {
return;
}
if(this._textLines.length > this.maxLines) {
return;
}
// Call parent class method
this.callSuper('onInput', e);
}
Note: maxWidth - to limit width
and maxLines - to limit height / lines in text box.
Edit: Fixed Syntax.
Related
i am trying to making zoomin images slideshow using pixijs on canvas from three images like this
I tried but not succeed. My question in how to add zoomin or zoomout image in pixijs for specific width and height, then second image and so on.
I am writing this code
var pixiapp;
var pixiloader = PIXI.Loader.shared;
initpixiapp();
function initpixiapp() {
pixiapp = new PIXI.Application({ width: 1280, height: 720 });
document.getElementById('canvas-container').appendChild(pixiapp.view);
pixiloader.add('images/img1.png').add('images/img2.png').add('images/img3.png').load(handleimagesload);
}
function handleimagesload() {
var imagessprites=[]
var img1 = new PIXI.Sprite(pixiloader.resources['images/img1.png'].texture);
var img2 = new PIXI.Sprite(pixiloader.resources['images/img2.png'].texture);
var img3 = new PIXI.Sprite(pixiloader.resources['images/img3.png'].texture);
imagessprites.push(img1)
imagessprites.push(img2)
imagessprites.push(img3);
for (let index = 0; index < imagessprites.length; index++) {
const element = imagessprites[index];
pixiapp.stage.addChild(element);
// here will the the code to run zoom image to specific width and heigth and then move to next image,
// here is my code, its only display zooming third image
var ticker = new PIXI.Ticker();
ticker.add(() => {
console.log('ticker called')
element.width += 1;
element.height += 1;
if(element.width==1500)
{
ticker.stop()
}
})
ticker.start()
}
}
One more things, how to display a box with text for three seconds before slideshow start.
Try something like this:
var pixiapp;
var pixiloader = PIXI.Loader.shared;
initpixiapp();
function initpixiapp() {
pixiapp = new PIXI.Application({ width: 1280, height: 720 });
document.getElementById('canvas-container').appendChild(pixiapp.view);
pixiloader
.add('images/img1.png')
.add('images/img2.png')
.add('images/img3.png')
.load(handleimagesload);
}
function handleimagesload() {
var imagessprites = [];
var img1 = new PIXI.Sprite(pixiloader.resources['images/img1.png'].texture);
var img2 = new PIXI.Sprite(pixiloader.resources['images/img2.png'].texture);
var img3 = new PIXI.Sprite(pixiloader.resources['images/img3.png'].texture);
imagessprites.push(img1);
imagessprites.push(img2);
imagessprites.push(img3);
// Put first image on stage:
var currentImageIndex = 0;
var currentImage = imagessprites[currentImageIndex];
pixiapp.stage.addChild(currentImage);
var ticker = new PIXI.Ticker();
// Start "main animation loop":
ticker.add(() => {
currentImage.width += 1;
currentImage.height += 1;
if (currentImage.width >= 1500) {
// remove current image from stage:
pixiapp.stage.removeChild(currentImage);
// Move to next image:
// Increase index - but if it reached maximum then go back to 0 (to first image)
currentImageIndex++;
if (currentImageIndex >= imagessprites.length) {
currentImageIndex = 0;
}
currentImage = imagessprites[currentImageIndex];
// Set initial width and height of image (TODO: adjust this)
currentImage.width = 1000;
currentImage.height = 1000;
// put image on stage:
pixiapp.stage.addChild(currentImage);
}
});
ticker.start()
}
We are facing issues in Xamarin grid layout, the sizes are defined in the OnAppearing method. every time if we call OnAppearing method, the size of grid is reducing continuously, not getting exact issue
here is the code
gridLayout.RowDefinitions.Add(new RowDefinition(){
Height = new GridLength(5, GridUnitType.Star),
});
gridLayout.RowDefinitions.Add(new RowDefinition(){
Height = new GridLength(5, GridUnitType.Star),
});
gridLayout.ColumnDefinitions.Add(new ColumnDefinition()
{
Width = new GridLength(2, GridUnitType.Star),
});
gridLayout.ColumnDefinitions.Add(new ColumnDefinition(){
Width = new GridLength(2, GridUnitType.Star),
});
gridLayout.ColumnDefinitions.Add(new ColumnDefinition(){
Width = new GridLength(2, GridUnitType.Star),
});
gridLayout.ColumnDefinitions.Add(new ColumnDefinition(){
Width = new GridLength(2, GridUnitType.Star),
});
gridLayout.ColumnDefinitions.Add(new ColumnDefinition(){
Width = new GridLength(2, GridUnitType.Star),
});
var productIndex = 0;
for (int rowIndex = 0; rowIndex < 2; rowIndex++)
{
for (int columnIndex = 0; columnIndex < 5; columnIndex++)
{
if (productIndex >= CategoryArray.Count)
{
break;
}
var category = CategoryArray[productIndex];
productIndex += 1;
var categoriesView = new CategoriesView
{
BackgroundColor = Color.White
};
if(category.Image == null){
categoriesView.CategoriesImage.Source = "category_logo";
}else{
categoriesView.CategoriesImage.Source = category.Image;
}
//categoriesView.BackgroundColor = Color.Olive;
categoriesView.TextLabel.Text = category.Name;
categoriesView.CategoryId = category.Id.ToString();
gridLayout.Children.Add(categoriesView, columnIndex, rowIndex);
//gridLayout.BackgroundColor = Color.Beige;
}
}
when ever OnAppearing is calling the page is reloading (the is the feature) and the grid size is reducing continuously.
You should declare the Grid structure only once, ideally in Constructor (or in XAML which is called in the constructor). Whenever OnAppearing is called, you are adding 2 new rows and 5 new columns to the existing Grid hence its size is reducing.
Also, by looking at the elements adding logic you've written, you should probably be using ListView with a Grid ViewCell and set the ItemsSource to an ObservableCollection so that it will update automatically
I continue my work on collaborative sketch tool and trying to add retina devices support. Currently i have following behavior if user creating drawing on ipad air:
small movie
Here is my code:
this.getZoomLevel = function (height) {
if (height > 1024) {
return 1024 / height;
} else {
return height / 1024;
}
};
this.calculateCanvasSize = function(pHeight, pWidth) {
var result = {
height: 0,
width: 0
};
while (result.width < pWidth - 1 && result.height < pHeight - 1) {
result.height = result.height + 1;
result.width = result.height * 4 / 3;
}
return result;
};
this.initCanvas = function () {
try {
var parent = document.getElementsByClassName('komaso-canvas-container')[0];
var canvasSize = this.calculateCanvasSize(parent.clientHeight, parent.clientWidth);
var canvasHtml = "<div id='wrapper-" + this.Id + "' class='whiteboard-canvas-wrapper' data-ng-show='CurrentPage.Id==" + this.Id + "'><canvas width='" + canvasSize.width + "' height='" + canvasSize.height + "' id='whiteboard-" + this.Id + "' class='whiteboard'><p>Your brower does not support Canvas/p></canvas></div>";
$(parent).append($compile(canvasHtml)(scope));
this.Canvaso = document.getElementById(this.HtmlId);
if (!this.Canvaso) {
console.log('Error: Cannot find the imageView canvas element!');
return;
}
if (!this.Canvaso.getContext) {
console.log('Error: no canvas.getContext!');
return;
}
this.FabricCanvas = new fabric.Canvas(this.HtmlId, { selectionColor: 'transparent' });
this.FabricCanvas.setWidth(canvasSize.width);
this.FabricCanvas.setHeight(canvasSize.height);
fabric.Object.prototype.transparentCorners = false;
this.FabricCanvas.on('mouse:down', this.onMouseDown);
this.FabricCanvas.on('mouse:up', this.onMouseUp);
this.FabricCanvas.on('mouse:move', this.onMouseMove);
this.FabricCanvas.on('object:added', this.onObjectAdded);
this.FabricCanvas.on('text:editing:exited', self.onTextObjectEdited);
if (window.devicePixelRatio !== 1) {
var c = this.FabricCanvas.getElement();
var w = c.width, h = c.height;
c.setAttribute('width', w * window.devicePixelRatio);
c.setAttribute('height', h * window.devicePixelRatio);
$(c).width(canvasSize.width);
$(c).height(canvasSize.height);
c.getContext('2d').scale(window.devicePixelRatio, window.devicePixelRatio);
}
this.FabricCanvas.setZoom(this.getZoomLevel(this.Canvaso.height));
this.ToggleTool(self.CurrentTool.ToolName);
this.WhiteboardInitiated = true;
} catch (e) {
console.log(e);
}
};
getZoomLevel returns value to pass into SetZoom method of fabric js canvas object. We decided to have all clients canvas aspects are 4:3 and default dimension is 1024*768. So based on this dimensions we calculation zoom factor.
calculateCanvasSize - returns width and height for canvas according to 4:3 rule.
If you have any idea about how to fix this wrong behavior then post your comment please. Thank you in advance!
I would suggest you yo update to a retina enabled version of fabricjs (grab 1.6.2).
If, for any reason you can't, i think the problem is here:
if (window.devicePixelRatio !== 1) {
var c = this.FabricCanvas.getElement();
...
c.getContext('2d').scale(window.devicePixelRatio, window.devicePixelRatio);
}
getContext return a new context. This is not the context where fabric is gonna render later. If you want to have retina enabled lowerCanvas you have to scale this.FabricCanvas.contextContainer that gets created and referenced on fabric.Canvas initialization.
I suggest you to switch to newer fabric anyway.
I'm referring to the official example on Phaser.io site, but have copied it here for reference below. What I want, and repeatedly fail to achieve is that the moving (with keyboard keys) starfield sprite would not collide with other vegies sprites.
I did go through the docs and looked here on SO and their forum, and it seems that the solutions should be easy enough; to just put the following code in the update() function:
game.world.bringToTop(sprite);
But, for some reason this is not working for me, so please tell me what I'm doing wrong.
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update });
function preload() {
game.load.image('sky', 'assets/skies/sky4.png');
game.load.image('starfield', 'assets/misc/starfield.jpg');
game.load.spritesheet('veggies', 'assets/sprites/fruitnveg64wh37.png', 64, 64);
}
var sprite;
var cursors;
var veggies;
function create() {
game.add.image(0, 0, 'sky');
// Enable p2 physics
game.physics.startSystem(Phaser.Physics.P2JS);
// Make things a bit more bouncey
game.physics.p2.defaultRestitution = 0.8;
// Add a sprite
sprite = game.add.tileSprite(300, 450, 200, 50, 'starfield');
// Enable if for physics. This creates a default rectangular body.
game.physics.p2.enable(sprite);
veggies = game.add.group();
veggies.enableBody = true;
veggies.physicsBodyType = Phaser.Physics.P2JS;
var vegFrames = [ 1, 3, 4, 8 ];
for (var i = 0; i < 10; i++)
{
var veg = veggies.create(game.world.randomX, game.world.randomY, 'veggies', game.rnd.pick(vegFrames));
veg.body.setCircle(26);
}
text = game.add.text(20, 20, 'move with arrow keys', { fill: '#ffffff' });
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
sprite.body.setZeroVelocity();
game.world.bringToTop(veggies);
if (cursors.left.isDown)
{
sprite.body.moveLeft(400);
sprite.tilePosition.x -= 8;
}
else if (cursors.right.isDown)
{
sprite.body.moveRight(400);
sprite.tilePosition.x += 8;
}
if (cursors.up.isDown)
{
sprite.body.moveUp(400);
sprite.tilePosition.y -= 8;
}
else if (cursors.down.isDown)
{
sprite.body.moveDown(400);
sprite.tilePosition.y += 8;
}
}
edit: Solution which worked in the end thanks to SirSandman's answer:
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('stars', 'assets/misc/starfield.jpg');
game.load.spritesheet('ship', 'assets/sprites/humstar.png', 32, 32);
game.load.image('panda', 'assets/sprites/spinObj_01.png');
game.load.image('sweet', 'assets/sprites/spinObj_06.png');
}
var ship;
var starfield;
var cursors;
function create() {
// Enable P2
game.physics.startSystem(Phaser.Physics.P2JS);
// Turn on impact events for the world, without this we get no collision callbacks
game.physics.p2.setImpactEvents(true);
game.physics.p2.restitution = 0.8;
// Create our collision groups. One for the player, one for the pandas
var playerCollisionGroup = game.physics.p2.createCollisionGroup();
var pandaCollisionGroup = game.physics.p2.createCollisionGroup();
// This part is vital if you want the objects with their own collision groups to still collide with the world bounds
// (which we do) - what this does is adjust the bounds to use its own collision group.
game.physics.p2.updateBoundsCollisionGroup();
starfield = game.add.tileSprite(0, 0, 800, 600, 'stars');
starfield.fixedToCamera = true;
var pandas = game.add.group();
pandas.enableBody = true;
pandas.physicsBodyType = Phaser.Physics.P2JS;
for (var i = 0; i < 4; i++)
{
var panda = pandas.create(game.world.randomX, game.world.randomY, 'panda');
panda.body.setRectangle(40, 40);
// Tell the panda to use the pandaCollisionGroup
panda.body.setCollisionGroup(pandaCollisionGroup);
// Pandas will collide against themselves and the player
// If you don't set this they'll not collide with anything.
// The first parameter is either an array or a single collision group.
panda.body.collides(pandaCollisionGroup);
panda.body.velocity.x = 500;
panda.body.velocity.y = 500;
}
// Create our ship sprite
ship = game.add.sprite(200, 200, 'ship');
ship.scale.set(2);
ship.smoothed = false;
ship.animations.add('fly', [0,1,2,3,4,5], 10, true);
ship.play('fly');
game.physics.p2.enable(ship, false);
ship.body.setCircle(28);
ship.body.fixedRotation = true;
// Set the ships collision group
ship.body.setCollisionGroup(playerCollisionGroup);
// The ship will collide with the pandas, and when it strikes one the hitPanda callback will fire, causing it to alpha out a bit
// When pandas collide with each other, nothing happens to them.
game.camera.follow(ship);
cursors = game.input.keyboard.createCursorKeys();
}
function hitPanda(body1, body2) {
// body1 is the space ship (as it's the body that owns the callback)
// body2 is the body it impacted with, in this case our panda
// As body2 is a Phaser.Physics.P2.Body object, you access its own (the sprite) via the sprite property:
body2.sprite.alpha -= 0.1;
}
function update() {
ship.body.setZeroVelocity();
if (cursors.left.isDown)
{
ship.body.moveLeft(200);
}
else if (cursors.right.isDown)
{
ship.body.moveRight(200);
}
if (cursors.up.isDown)
{
ship.body.moveUp(200);
}
else if (cursors.down.isDown)
{
ship.body.moveDown(200);
}
if (!game.camera.atLimit.x)
{
starfield.tilePosition.x += (ship.body.velocity.x * 16) * game.time.physicsElapsed;
}
if (!game.camera.atLimit.y)
{
starfield.tilePosition.y += (ship.body.velocity.y * 16) * game.time.physicsElapsed;
}
}
function render() {
game.debug.text('Collide with the Pandas!', 32, 32);
}
I P2 you have to set the Collisiongroups in contrast to arcarde.
I think you have to set a collisiongroup for the sprite like that:
var veggCollisionGroup = game.physics.p2.createCollisionGroup();
and then define with which other groups this group shell collide like that in the Loop:
veggies.body.setCollisionGroup(veggCollisionGroup);
veggies.body.collides(veggCollisionGroup);
And then the your tilesprite should collide with your other tilesprites.
Source:
http://phaser.io/examples/v2/p2-physics/collision-groups
if i should be wrong you will find your answer in the examples. :)
I cannot get it working although I am very close to an acceptable solution.
I can drag the Dock if it is only a rectangle. But if I add a node (e.g. an image) to this dock I am not able to get a working solution.
Here is my code:
public class Dock extends CustomNode {
// initialize this with an 64x64 image of your choice
// via ImageView { image: Image {..}}
public var content: Node[];
public var width = 64;
public var height = 64;
public var xOffset: Number = 0;
public var yOffset: Number = 0;
var imgX: Number = 0;
var imgY: Number = 0;
var distX: Number;
var distY: Number;
public var rasterX = function (n: Number): Number {
var MAX = 4 * 64;
if (n < 0) {
return 0
} else if (n > MAX) {
return MAX
} else
return n
}
public var rasterY = rasterX;
override protected function create(): Node {
Group {
// if we place the translate here then the whole dock will flicker
//translateX: bind imgX;
//translateY: bind imgY;
content: [
Rectangle {
// ... and here 'content' logically won't be dragged
translateX: bind imgX;
translateY: bind imgY;
height: bind height
width: bind width
fill: Color.LIGHTGRAY
strokeWidth: 4
stroke: Color.BLACK
}, content]
onMousePressed: function (e: MouseEvent): Void {
xOffset = e.x;
yOffset = e.y;
// Calculate the distance of the mouse point from the image
// top-left corner which will always come out as positive value
distX = e.x - imgX;
distY = e.y - imgY;
}
onMouseDragged: function (e: MouseEvent): Void {
// Find out the new image postion by subtracting the distance
// part from the mouse point.
imgX = rasterX(e.x - distX);
imgY = rasterY(e.y - distY);
}
}
}
I tried blocksMouse:true on different nodes, tried it with mouseReleased etc but I coudn't get properly working solution. Do you have any pointers/tips on how this it done correctly?
Finally sorted it out from this example, but you don't need to follow the tutorial (I got an exception!?) - simply download the sources or use:
public class Dock extends CustomNode {
public var content: Node[];
public var width = 64;
public var height = 64;
public var xOffset: Number = 0;
public var yOffset: Number = 0;
public var rasterX = function (n: Number): Number {
// the following code is for the 'grid' which can be avoided of course
var n2 = n - n mod 64;
var MAX = 4 * 64;
if (n2 < 0) {
return 0
} else if (n2 > MAX) {
return MAX
} else
return n2
}
public var rasterY = rasterX;
override protected function create(): Node {
var node: Group;
node = Group {
content: [
Rectangle {
height: bind height
width: bind width
arcHeight: 10
arcWidth: 10
fill: Color.LIGHTGRAY
strokeWidth: 4
stroke: Color.BLACK
effect: DropShadow {
offsetX: 5
offsetY: 5
color: Color.DARKGRAY
radius: 10
}
}, content]
onMousePressed: function (e: MouseEvent): Void {
xOffset = e.sceneX - node.translateX;
yOffset = e.sceneY - node.translateY;
}
onMouseDragged: function (e: MouseEvent): Void {
node.translateX = rasterX(e.sceneX - yOffset);
node.translateY = rasterY(e.sceneY - yOffset);
}
}
}
}