How to scale a text object? - javascript

var loginText = new Kinetic.Text({
x: 5,
y: 5,
text: 'login: someLogin',
fontSize: 14,
fontFamily: 'Arial',
fill: 'green'
});
loginText.scaleX(4);
loginText.scaleY(4);
I'am trying to scale a Kinetic.Text object, but nothing happens, I don't know if its possible, I've also tried this:
loginText.fillPatternScale({
x: 4
y: 4
});
but still nothings happens, any ideas?

The answer provided by markE should work.
Here is the complete code and a fiddle example where you can see it work: http://jsfiddle.net/J8vju/
var stage = new Kinetic.Stage({
width: 400,
height: 400,
container: 'container'
});
var layer = new Kinetic.Layer();
var loginText = new Kinetic.Text({
x: 5,
y: 5,
text: 'login: someLogin',
fontSize: 14,
fontFamily: 'Arial',
fill: 'green'
});
var loginText2 = loginText.clone({y: 200});
loginText.scaleX(4);
loginText.scaleY(4);
layer.add(loginText);
layer.add(loginText2);
stage.add(layer);
stage.draw();

You must add the text object to the layer and then layer.draw to redisplay the layer:
var loginText = new Kinetic.Text({
x: 5,
y: 5,
text: 'login: someLogin',
fontSize: 14,
fontFamily: 'Arial',
fill: 'green'
});
loginText.scaleX(4);
loginText.scaleY(4);
layer.add(loginText);
layer.draw();

scaleX should work check out this example here just add this line
simpleText.scaleX(2);
as line number thirty(30) to this example. You will see the scaleX effect on the text working in the example.Make sure you are scaling it before adding line to the layar.Hope that helps.

Related

Snapsvg Element Display Order Issue

I am using snapsvg.io library and unsure why the following code is not displaying my rectangle with text over my svg line element.
Based on the order I have placed my code, I assumed that my rect/text group would be on top of my line but unfortunately this is not the case and can't figure out what I am doing wrong.
The code sample I have is as follows:
line = s.line(trunkLeftPos, 100, trunkLeftPos, 440);
line.attr({
stroke: error1 ? alert : error2 ? alert : stroke,
strokeWidth: 1
});
var text = s.text(trunkLeftPos + 4, trunkLeftTextPos, 'my text');
text.attr({
fill: error1 ? alert : error2 ? alert : textColorOK,
fontSize: '11px',
'font-weight': '600',
'font-family': 'Arial Narrow, sans-serif',
'text-anchor': 'start',
cursor: error1 ? 'pointer' : error2 ? 'pointer' : 'default',
});
var rect_bg = s.rect(trunkLeftPos + 2, trunkLeftTextPos - 10, 50, 13, 0, 0).attr({fill: "rgb(236, 240, 241)",stroke: "#1f2c39",strokeWidth: 1});
var g0 = s.g(rect_bg,text);
What I have tried to show you here is the order of play where I expected my g0 element to be placed over my line element but from what I can see, the vertical line appears over the top of my g0 element and not below it.
Any ideas?
Just created a codepen example and looks like all worked fine with the sample test within my codepen based on the same display order I have presented above.
Looks like I need to look at my values as indicated by #Sphinxxx
See: http://codepen.io/tonyf/pen/KMzvRe?editors=1010
<svg id="svg" width="400" height="400"></svg>
const s = Snap("#svg");
const line = s.line(0, 0, 40, 200);
line.attr({
stroke: 'green',
strokeWidth: 2
});
const text = s.text(0 + 14, 100, 'my text');
text.attr({
fill: 'red',
fontSize: '11px',
'font-weight': '600',
'font-family': 'Arial Narrow, sans-serif',
'text-anchor': 'start'
});
const rect_bg = s.rect(0 + 10, 90, 50, 13, 0, 0).attr({
fill: "#fff",
stroke: "#1f2c39",
strokeWidth: 0.5
});
const g0 = s.g(rect_bg,text);

deleting a line after selecting the desired line on clicking a button kinetic js

var line = new Kinetic.Line({ points: [415, 115,617,234], stroke: 'gray', tension: 2});
line.addEventListener('click',function(e){
debugger;
// alert(e.x+'.'+ e.y);
// popup;
});
On clicking the line it should be selected.on clicking other place expect line it should be deselected.after selecting the line if i pressed a delete button the line should get destroyed how to do it.
Not exact as your requirement, but you double click on line, line will be removed from layer
here is demo.
var stage = new Kinetic.Stage({
container: 'container',
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
var redLine = new Kinetic.Line({
points: [73, 70, 340, 23, 450, 60, 500, 20],
stroke: 'red',
strokeWidth: 15,
lineCap: 'round',
lineJoin: 'round'
});
redLine.on("dblclick",function(){
this.destroy();
layer.draw();
});
layer.add(redLine);
stage.add(layer);

How to pass an event on to the nodes below in kinetics

question that is very similar but answer doesn't work in my situation
I have a circle and I want to pass the click event to any objects that are underneath it.
I have tried:
setting cancelbubble to false.
setting the cirle layer to listening= false doesn't work for me because the circle is then no longer draggable.
I have made a fiddle here
var stage = new Kinetic.Stage({
container: 'container',
width: 578,
height: 400
});
var layer = new Kinetic.Layer();
var background = new Kinetic.Layer();
var colorPentagon = new Kinetic.RegularPolygon({
x: 80,
y: stage.getHeight() / 2,
sides: 5,
radius: 70,
fill: 'red',
stroke: 'black',
strokeWidth: 4,
draggable: true
});
colorPentagon.on('click', function(evt){ alert("pentagon");});
var linearGradPentagon = new Kinetic.RegularPolygon({
x: 360,
y: stage.height()/2,
sides: 5,
radius: 70,
fillLinearGradientStartPoint: {x:-50, y:-50},
fillLinearGradientEndPoint: {x:50,y:50},
fillLinearGradientColorStops: [0, 'red', 1, 'yellow'],
stroke: 'black',
strokeWidth: 4,
draggable: true
});
linearGradPentagon.on('click', function(evt){ alert("pentagon");});
var radialGradPentagon = new Kinetic.RegularPolygon({
x: 500,
y: stage.height()/2,
sides: 5,
radius: 70,
fillRadialGradientEndRadius: 70,
fillRadialGradientColorStops: [0, 'red', 0.5, 'yellow', 1, 'blue'],
stroke: 'black',
strokeWidth: 4,
draggable: true
});
radialGradPentagon.on('click', function(evt){ alert("pentagon");});
background.add(colorPentagon);
//background.add(patternPentagon);
background.add(linearGradPentagon);
background.add(radialGradPentagon);
stage.add(background);
var hideCircle = new Kinetic.Circle({
x: stage.width()/2,
y: stage.height()/2,
radius: 650,
stroke: 'black',
strokeWidth: 1000,
draggable:true
});
hideCircle.on('click', function(evt){ alert("click");});
// add the shape to the layer
layer.add(hideCircle);
// add the layer to the stage
stage.add(layer);
Currently to get it to work I have to take the click coordinates and do my own detection. It works but I was hoping for something more elegant.
Here's how to pass your click event to the bottom layer nodes:
You can listen for clicks on the stage
Determine if the click is inside any node on the bottom layer
If the click was inside a node, fire the click event on that node.
Here's example code and a Demo: http://jsfiddle.net/m1erickson/sFX8y/
stage.on('contentClick',function(e){
// get the mouse position
var pos=stage.getPointerPosition();
// fetch the node on the bottom layer that is under the mouse, if any.
var hitThisNode=background.getIntersection(pos);
// if a node was hit, fire the click event on that node
if(hitThisNode){
hitThisNode.fire("click",e,true);
}
});

KineticJS / Javascript dynamic creation and immediate dragging

What I want do is dynamically create a shape on mousedown, and then immediately have it (the shape) follow the mouse cursor until mouseup sets it in place.
Here is what I have so far and it's not working for me.
addNegativeButton.on('mousedown', function(){
var userPos = stage.getUserPosition();
shapesLayer.add(new Kinetic.Image({
image: imageObj,
x: userPos.x,
y: userPos.y,
height: 25,
width: 25,
rotation: 1,
draggable: true,
offset: 12
}));
var last = shapesLayer.getChildren()[shapesLayer.getChildren().length -1];
stage.on("mouseup", function(){
last.setAbsolutePosition(stage.getUserPosition());
stage.off("mouseup");
});
To reiterate:
What I have is an 'addNegativeButton' which when clicked creates a shape.
But I want it to follow the mouse cursor until the click is released.
http://jsfiddle.net/CPrEe/37/
Any suggestions?
Turns out its rather simple ;)
After you add the element/shape, all you have to do is use the simulate function to simulate mouse down and it drags....
var stage = new Kinetic.Stage({
container: 'container',
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
var rect = new Kinetic.Rect({
x: 20,
y: 20,
width: 100,
height: 50,
fill: 'green',
stroke: 'black',
strokeWidth: 4
});
//What I really want here is to start dragging the circle until the
//user releases the mouse, which would then place the circle.
rect.on('mousedown', function(evt) {
var userPos = stage.getUserPosition();
var latestElement = new Kinetic.Circle({
x: userPos.x,
y: userPos.y,
radius: 20,
fill: 'red',
stroke: 'black',
strokeWidth: 4,
draggable: true
})
layer.add(latestElement);
// Here's the bit you want. After adding the circle (with draggable:true) simulate the mousedown event on it which will initiate the drag
latestElement.simulate('mousedown');
layer.draw();
});
layer.add(rect);
stage.add(layer);​
http://jsfiddle.net/CPrEe/38/
http://kineticjs.com/docs/symbols/Kinetic.Node.php#simulate

kineticjs rect fillText

I have KineticJS rect already fill with 'blue' color, I want to show Text on this rect box. But my below code not showing anything. Also I need to assign this label text on button click, the code for the same is also below.
drawShape = new Kinetic.Rect({
id: shapeId,
name: shapeName,
x: 0,
y: 0,
width: 150,
height: 40,
fill: "#00D2FF",
stroke: "black",
strokeWidth: 3,
fillText: "Step" + stepNumber
});
function OnApplyPropertiesClick(){
drawShape.fillText(document.getElementById("txtLabel").value);
}
any help on this?? please.
Thanks
Biru
I don't think you can add text to a rectangle object, only a fill type of:
[color, linear gradient, radial gradient, or pattern]
To solve this i created a group, then added a rectangle and text object to it, when i wanted to create a rectangle with text on it:
var group = new Kinetic.Group({
});
var rectangle = new Kinetic.Rect({
x: 5,
y: 5,
width: 80,
height: 35,
fill: "green",
stroke: "black",
strokeWidth: 2
});
var rectangleText = new Kinetic.Text({
x: 15,
y: 10,
text: 'test',
fontSize: 20,
fontFamily: 'Calibri',
textFill: 'black'
});
group.add(rectangle);
group.add(rectangleText);
layer.add(group);
You should be able to adapt your listener to then apply it to this.
I've done a jsfiddle to show what i mean:
http://jsfiddle.net/B85Ff/
Have you added drawShape to any layer yet? Doesn't appear in the snippet you posted.
Also, you need to draw the layer (like a refresh) at the end of your OnApplyPropertiesClick() function.

Categories

Resources