JavaScript Raphael, adding text on top of rectangle through different functions - javascript

I want to let function B work without having set the instruction code to function A. In praktical terms, to show text on top of a rectangle.
In this question Button A is used for creating the 'paper' and a rectangle(which uses the Raphael library). Button B for adding text on top of the rectangle. The HTML code looks like this:
<button onClick="func.A()">Func A</button>
<button onClick="func.B()">Func B</button>
The JavaScript code looks like this:
var func = (function functie($) {
return {
A: function() {
// Creates canvas 320 × 200 at 10, 50
var paper = Raphael(10, 50, 320, 200);
// Creates rectangle
var bg = paper.rect(0, 0, 320, 200);
// Sets the fill attribute of the circle to red (#f00)
bg.attr("fill", "#f00");
// Sets the stroke attribute of the circle to white
bg.attr("stroke", "#fff");
},
B: function() {
var t = paper.text(40, 15, "");
t.attr('text',"new text here");
t.attr();
};
})();
The problem is that when the instruction code of function B (var t = paper.text(40, 15, ""); and so on), are placed in function B, that the text i try to add won't be added to the rectangle.
If the instruction code of function B are placed in function A, then it will work, but this is not what i want. I want to let function B work without having set the instruction code to function A.
I hope this problem is clear enough to understand.

When you declare "var paper" in function A, that variable is local to function A. If you want to share state information between function calls, you have to store the state information in properties of your object, rather than local variables:
var func = (function functie($) {
return {
paper: null,
A: function() {
// Creates canvas 320 × 200 at 10, 50
this.paper = Raphael(10, 50, 320, 200);
// Creates rectangle
var bg = paper.rect(0, 0, 320, 200);
// Sets the fill attribute of the circle to red (#f00)
bg.attr("fill", "#f00");
// Sets the stroke attribute of the circle to white
bg.attr("stroke", "#fff");
},
B: function() {
var t = this.paper.text(40, 15, "");
t.attr('text',"new text here");
t.attr();
};
})();

Related

How do you get the width value of a text element via a plugin in XD?

I am trying to build a plugin for XD to automate the creation of design elements. I have written a function to build the button (first picture below) using a text element and a rectangle. The only problem with this is that shape of the rectangle is not based on the width of the text, so when the text is longer you end up with the text overlapping the rectangle (see second picture below).
I have been trying to figure out how to get the width value of the text element so that I can size the rectangle appropriately. Can anyone help me with this?
I have figured out that by adding the line console.log (selection.items); I can output a full list of properties to the log, but how do I access the width property?
Here is the log output showing the rectangle and text elements...
[ Text ('Submit Button') {
width: 113, height: 20
global X,Y: -3, -74
parent: Artboard ('iPad – 1')
fill: ff000000
},
Rectangle ('Rectangle 6') {
width: 100, height: 50
global X,Y: -3, -58
parent: Artboard ('iPad – 1')
stroke: ff2d3494
} ]
and here is my full code...
const {Rectangle, Color, Text, Selection} = require("scenegraph");
let commands = require("commands");
function createButton(selection) {
// Create the outline of the button
const newButtonOutline = new Rectangle();
newButtonOutline.width = 100;
newButtonOutline.height = 50;
newButtonOutline.stroke = new Color("#2D3494");
newButtonOutline.strokeWidth = 2;
newButtonOutline.cornerRadii = {
topLeft: 10,
topRight: 10,
bottomRight: 10,
bottomLeft: 10
};
// Create the text for the button
const newButtonLabel = new Text();
newButtonLabel.text = "Submit Button";
newButtonLabel.fontSize = 18;
newButtonLabel.fill = new Color("#000000");
// Add the text and outline to the artboard
selection.insertionParent.addChild(newButtonOutline);
newButtonOutline.moveInParentCoordinates(100, 100);
selection.insertionParent.addChild(newButtonLabel);
newButtonLabel.moveInParentCoordinates(100, 100);
selection.items = [newButtonLabel, newButtonOutline];
console.log (selection.items);
//align the button elements and group together
commands.alignHorizontalCenter();
commands.alignVerticalCenter();
commands.group();
}
I hope someone can help!
So it turns out to get the width of a text node you need to get it from the localBounds, something like...
textNode.localBounds.width
or to get the width of a graphic node it is just...
graphicNode.width

How to program pressing different buttons for different effects in Javascript

I am taking a beginner class in coding and need help with an issue I'm coming across in a project that I am making...
function start() {
//Creates the jester image, describing text and the selectable key for them
var rect = new Rectangle(200, 120);
rect.setPosition(0, 0);
rect.setColor(Color.grey);
add(rect);
var jester = new WebImage("data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxITEhMTExIVFhUXFhUWFRcXFRUWFRcVFxUXFhUVFRUYHSggGBolHRUVITEhJSkrLi4uFx8zODMtNygtLisBCgoKDg0OGxAQGy0lICUtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS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UGITsY4xkjMNCiKIi4I4Rit2jaoqqHc3TUT23sub9XYM2K5b3XS2YlpZc+65hcTmB05Co0RyRVFMj3TqneS3y7rXp/AZKhwNiI+Xd/ZXJmBxsADRZSnTIRi2VWOnlO1ym1FTtDf5jTdO4aYN0Aui4qTO5rA3U6f8A1TbSRRLiViSnafhzD32WR4a7e66dimDxNpbNbqzzbak7FU9zW91PBnjlVxAqlsb4B1YIWtjkBFha/CfxdY07iWh7b2v6KjviaR3ULsObceX8Fo5A4lwxPrOBg1JJO1tVX+pOqWzxZGNNyNT2S91A3gXWfcrkNA39F1gcWVeSldZDS0bldHYU69st7eiJiwTNu0D3Qs7jYD9m2MPjlMT3ANO1/wAguv8A33Vttj9VyWbpu5u0ua8G4sOUynGIkxuDmeTQi583v2TWdR2Gkq2nnVGNmAXLqXGq0Mt4TM22bPoPwujJqyre1o8Vjb6ENab/APsUeaR3FhXXTo5JW2NyG2Njtqq7FC47A6eibMp/DFiY3O5JP691HNmaAW2ab6WN1CSUnZSOkBwUrnEho19dERJh0jdfLb3uVtlc73vqbjnvYrapgc22Y6+/HyUuagU4tghaefyRMDbt0Gv4LeNrCQHH5gHRFU0DQdD9Ujk5qkFLi9mkkRbGLjUn5WVL6s+IDhdAxK3ht91zzqR95beiORuMCGZ6YgyBYjfu69WT3GY+TFeN/F/5K2YN/TCxYvZl4PUx9hTOVUepNysWKngGTosvT/8AQZ7IibhYsWYRdHlP8Y9wm1J/1Y9j+SxYvP8AWfl/xkTn/IsVX/QPzVC7rFih/hX+r9o/zFxdM1CIh5WLF7DKI1bt8167dYsXAGFJsVLUfCPcLFiXyMa0u5+aLi591ixEB4PhP+4KcbfL9FixcBA5/pj2P5qKbZYsS+RjMJ3d7FFu2WLFhz/5kf3L4/xYPBz8/wAkyg+EfvhYsVMXQnhEtZ/Sb8/zXNeo/wDqT7BYsVc3TM8/xI1ixYvNMh//2Q==");
jester.setSize(200, 120);
jester.setPosition(0, 0);
add(jester);
var txt = new Text("Describing text!", "15pt Arial");
txt.setPosition(230, 30);
txt.setColor(Color.blue);
add(txt);
//Creates the king image, describing text and the selectable key for them
var rect = new Rectangle(200, 120);
rect.setPosition(0, 120);
rect.setColor(Color.green);
add(rect);
var king = new WebImage("data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxITEhMTExIVFhUXFhUWFRcXFRUWFRcVFxUXFhUVFRUYHSggGBolHRUVITEhJSkrLi4uFx8zODMtNygtLisBCgoKDg0OGxAQGy0lICUtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLf/AABEIAOEA4QMBIgACEQEDEQH/xAAcAAACAgMBAQAAAAAAAAAAAAAEBQMGAAIHAQj/xAA7EAABAwIFAgQDBgUDBQEAAAABAAIDBBEFEiExQQZREyJhcTKBkQcUobHB8BUjM0LRUnLhNGKCkvEW/8QAGQEAAwEBAQAAAAAAAAAAAAAAAQIDBAAF/8QALhEAAgICAgEEAQIEBwAAAAAAAAECEQMhEjFBBBMiUTIUcWHB8PEFM0KBgpGh/9oADAMBAAIRAxEAPwCtPh8hyygttsdx7JEDp6Jq+gksfKSO6GNDxyVjk5S2kbZY5z3FdEdNhocQ5MazDZJA0NAIuAdbEC++qhoqV0Y0d+/ZG0GJSOk8MgWALr86KWSeJrlJNNEJwS/KNHtSP5wBBABaBxoEp6jqhl/3Pv8ARN3Yk6xDrPFzuBoOwO91VeoHNc9mQ6AatO7SfzHqs2DFHmnF9fZFr6IPCs0uCv2E4Xaki8KZhjDM8mYhuWY6n33VChqdQyysFQAxjIx/uctOd1Gn5BGVB9DWQwtc57RI8E2J+H3CQYvi8tQbuPlHwtGw9lFXSF5yjYI+jom5NVNfCPJhllbXHwKaGHMdduUbPr5W7BGRQt2A05W1QwNFmjU7BGMk5cmTvwKS539Ng1KybCmwgOkddx1I7KxYXhwiBe7V52CArMPMjyXa8rvf5PX4/f2O6WgfCntdsLBZiWI28jUPWTFnkYLLbD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KngGTosvT/8AQZ7IibhYsWYRdHlP8Y9wm1J/1Y9j+SxYvP8AWfl/xkTn/IsVX/QPzVC7rFih/hX+r9o/zFxdM1CIh5WLF7DKI1bt8167dYsXAGFJsVLUfCPcLFiXyMa0u5+aLi591ixEB4PhP+4KcbfL9FixcBA5/pj2P5qKbZYsS+RjMJ3d7FFu2WLFhz/5kf3L4/xYPBz8/wAkyg+EfvhYsVMXQnhEtZ/Sb8/zXNeo/wDqT7BYsVc3TM8/xI1ixYvNMh//2Q==");
king.setSize(200, 120);
king.setPosition(0, 120);
add(king);
var txt = new Text("Describing text!", "15pt Arial");
txt.setPosition(230, 140);
txt.setColor(Color.blue);
add(txt);
keyDownMethod(jesterSelect);
keyDownMethod(kingSelect);
}
function jesterSelect(e) {
if(e.keyCode == Keyboard.letter('J')){
removeAll();
var circle = new Circle(50);
circle.setPosition(100, 200);
circle.setColor(Color.blue);
add(circle);
}
}
function kingSelect(e) {
if(e.keyCode == Keyboard.letter('N')){
removeAll();
var circle = new Circle(50);
circle.setPosition(100, 200);
circle.setColor(Color.red);
add(circle);
}
}
Right now I have images displayed of a jester and a king and depending on what button you press you are directed to new "slide". As of now, the program only responds when I press the "k" key and doesn't respond and create the blue circle when I press the "j" key. I've determined that whatever is put last in the start function determines what actually works, but I want the user to be able to press any of the interactive keys and be displayed with a different outcome. I would appreciate any help since I've been stuck on this for a while now, thank you!
You need to capture the keydown event.
Add this to your init code:
window.addEventListener("keydown", keyHandler);
and modify this block for all of your key events:
function KeyHandler(e)
{
switch (e.keyCode)
{
case 32: // space
{
// dostuff
break;
}
case 37: // left arrow
{
// dostuff
break;
}
}
}

Unable to clear the canvas

I don't know why, but the clearRect() does not work.
This is a function that is executed every time I click on a panel or every time I change the thickness' value.
//When click on a menu section
$(".menuSection").click(function() {
//Show hide the selected item
$(this).next().toggle();
//hide all the other panels
$(this).next().siblings(".panel").hide();
//To update the values in the colors panel when selected
if ( $(this).prop("id") == "colors" ) {
changeColorBySliders();
}
if ( $(this).prop("id") == "pen" ) {
updatePreview();
}
});
//when thickness slider change:
$("#penPanel input[type=range]").on("input", updatePreview);
The fact is that it draws the line, but if I click it again it's doesn't reset.
var $preview = $("#preview");
var contextPreview = $preview[0].getContext("2d");
//Function to update the pen's preview
function updatePreview() {
console.log($("#thickness").val());
var rgba = $("#mainCanvas").css("background-color");
$("#preview").css("background-color", rgba);
//Resetting the preview
contextPreview.clearRect(0, 0, $preview.width, $preview.height);
//Drawing an example path
contextPreview.beginPath();
contextPreview.moveTo(50, 30);
contextPreview.lineTo(100, 110);
contextPreview.quadraticCurveTo(115, 120, 125, 80);
contextPreview.bezierCurveTo(145, -40, 150, 120, 200, 95);
contextPreview.lineTo(250, 65);
contextPreview.lineWidth = $("#thickness").val();
contextPreview.strokeStyle = color;
contextPreview.stroke();
contextPreview.closePath();
}
Any help?
Solution
I solved the problem using this kind of declaration:
var preview = document.getElementById("preview");
var contextPreview = preview.getContext("2d");
contextPreview.clearRect(0, 0, preview.width, preview.height);
Insted of the jquery one:
var $preview = $("#preview");
var contextPreview = $preview[0].getContext("2d");
Why?
it's because $preview is a jQuery object, $preview.width is therefore a function, so when you called clearRect(), you were actually calling contextPreview.clearRect(0,0, function, function) so your rect dimensions were undefined or 0 so it wasn't clearing a single pixel.
So you can still use jQuery but be sure you call contextPreview.clearRect(0,0, $preview[0].width, $preview[0].height)
var preview = document.getElementById("preview");
var contextPreview = preview.getContext("2d");
contextPreview.clearRect(0,0, $preview[0].width, $preview[0].height)
Special Thanks to Kaiido.

How to put image inside a circle with Raphael Js

I'm trying to put an image inside a circle but no success. This is my code:
//Elms.raphael() is my stage.
var circle = Elms.raphael().circle( 730, 200, 0 );
circle.attr( { fill : 'url(myImg.jpg)' } );
setTimeout( function()
{
circle.animate( { 'stroke' : '#000', r : 90, 'stroke-width' : '5' }, 300 );
}, 250 );
Instead of put the image in the circle It get colored with black ("#333"). I also tried to make an image-object but still doesn't work.
A little help please?
Another way to do, if you have separate image and want to bring it over you circle object.
This makes the whole image appear with smaller size that fits you circle. DEMO
var r = new Raphael(10,10, 500, 500);
var c = r.circle(200, 200, 80).attr({stroke: 'red', "stroke-width" : 3});
var img = r.image("http://www.eatyourcareer.com/wp-content/uploads/2012/06/ok-256x2561.png", 100, 105, 200, 200);
Here's a link to how I created a circle with an image in it:
jsfiddle
var paper = Raphael(document.getElementById("test"), 320, 200);
var circle = paper.circle(100, 100, 50);
circle.attr({
fill: 'url(http://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/SIPI_Jelly_Beans_4.1.07.tiff/lossy-page1-220px-SIPI_Jelly_Beans_4.1.07.tiff.jpg)'
});
I left the animate out entirely to keep it as basic as I could. It seems to work fine and is very similar to your code. If you cannot see it in the example it may be a browser issue.

CodeMirror - Is it possible to scroll to a line so that it is in the middle of window?

I'm highlighting lines of HTML code in CodeMirror and I want to add an anchor that scroll CodeMirror editor to a given line.
I can scroll to a line X via setCursor method. But I would like to have the line X in the middle of CodeMirror window. Can I do that? I studied the API and demos but with no luck.
Thanks!
This one should work:
var editor = CodeMirror.fromTextArea(...);
function jumpToLine(i) {
var t = editor.charCoords({line: i, ch: 0}, "local").top;
var middleHeight = editor.getScrollerElement().offsetHeight / 2;
editor.scrollTo(null, t - middleHeight - 5);
}
It is very simple.
Init:
var editor = CodeMirror.fromTextArea(...);
If you want current position to set it later, you can use
editor.getScrollInfo();
It returns an JavaScript object:
{
"left": 0,
"top": 0,
"height": 500,
"width": 500,
"clientHeight": 300,
"clientWidth": 200
}
now you can set set editor scroll position using:
editor.scrollTo(left,top);
You want https://codemirror.net/doc/manual.html#scrollIntoView
Notice the optional margin parameter which should do what you want:
cm.scrollIntoView(what: {line, ch}|{left, top, right, bottom}|{from, to}|null, ?margin: number)
Your code would be something like:
cm.scrollIntoView({line:50, char:5}, 200)
Initialization:
var editor = CodeMirror.fromTextArea(...);
Function to show a line in the middle of editor:
function jumpToLine(i) {
// editor.getLineHandle does not help as it does not return the reference of line.
editor.setCursor(i);
window.setTimeout(function() {
editor.setLineClass(i, null, "center-me");
var line = $('.CodeMirror-lines .center-me');
var h = line.parent();
$('.CodeMirror-scroll').scrollTop(0).scrollTop(line.offset().top - $('.CodeMirror-scroll').offset().top - Math.round($('.CodeMirror-scroll').height()/2));
}, 200);
}

Categories

Resources