How to assign value of variable to another method in javascript? - variables

my code is as follow
function myFunction(a,b,d)
{
var R = a;
var G = b ;
var B = d ;
var L = "(";
var C = ",";
var Rp = ")";
var E = L ;
var ic = '"';
var prefix = "RGB";
var color = ic+prefix + L + R+ C + G + C + B + Rp+ic;
alert(color)
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle = color;
}
if i call the above function for example myFunction(10,20,30); alert(color); prints "rgb(10,20,30)" but i don't know how to assign the value of color to ctx.fillStyle
Since i want to make dynamic application i need pass different value of color , right now the code doesn't work and ctx.fillStyle = color; is printed when code is executed. I will be thankful if some one can tell me how to assign the value of color to ctx.fillStyle method.

You are able to reference a variable for the desired color value as you're wanting to do, so long as the value being provided is a valid color value.
Based off of your provided code, it appears that you are using RGB instead of rgb.

First of all, "color" is a string variable here and to use it as a code segment use eval(); function. Secondly, set some visible area in the canvas to see if the code is really working i.e. ctx.fillRect(). Lastly, make sure your document type is declared as <!DOCTYPE html> which I hope is .
Here is the working version of the code (works for me in FF 11.0):
<!DOCTYPE html>
<html>
<head>
<script >
function myFunction(a,b,d)
{
var R = a;
var G = b;
var B = d;
var L = "(";
var C = ",";
var Rp = ")";
var E = L ;
var ic = '"';
var prefix = "RGB";
var color = ic+prefix + L + R+ C + G + C + B + Rp+ic;
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle=eval(color); //convert string to code.
ctx.fillRect(0,0,150,75);//any size greater than 0
}
</script>
<body>
<button onclick="myFunction(200,0,0);">click me</button>
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #c3c3c3;">
</body>
</html>

Related

adobe illustrator script- rectangle resize

×™ello everybody! I tried to create a script that creats a rectangle of a given size, make a group and a clipping mask and after all that resize it to a diffrent size in mm.
for example I have a graphic with the name "fish400" and I clip it in a rectangle of 400X400 that I create with the script. so far so good. my problem is that I want to resize the clipping with all it's content to 382. when I set the height to be the rec. size-18 it gets the height of 385.1
I'm not a very skilled programmer and the script could be written better but I really don't get my mistake.
here is my code:
var doc = app.activeDocument;
var layers = doc.layers;
var myLayer = layers["Layer 1"]; //this defines the layer that you want to get the selection from
var vals = 0;
var tzim = 0;
var side = 0; //width || height
//mm convertor
function mm(n) {return n * 2.83464566929134;}
doc.selection = null; //ensure there is nothing in the document selected already. this way you only get the selection you want.
var found = false;
for(var a=0 ;a<doc.groupItems.length; a++)
{
if (doc.groupItems[a].name == "fish400") {vals = mm(400); tzim = mm(18); side = 1; found = true;}
}
if (found = true)
{
var rect = doc.pathItems.rectangle(0,0,vals,vals);
app.executeMenuCommand("selectall");
var groupItem = doc.groupItems.add();
var count = selection.length;
for(var i = 0; i < count; i++)
{
var item = selection[selection.length - 1];
item.moveToBeginning(groupItem);
}
groupItem.clipped = true;
item.top = center_point[0] + (item.height/2);
item.left = center_point[1] - (item.width/2);
if (side == 1) {groupItem.height -= tzim;} else if (side == 0) {groupItem.width -= tzim;}
}
If your script works fine for you (it doesn't work for me, though) and all you want is to add resizing for your picture from 400x400 to 382x382 mm you can just to add at the end of your script these lines:
var k = 382/400*100;
groupItem.resize(k,k);
Or:
app.executeMenuCommand("selectall");
var sel = app.selection[0];
var k = 382/400*100;
sel.resize(k,k);

find and replace parts of a string in sql

I'm using a Telerik RadEditor to insert text with rich formatting. The problem is that my rdlc reports can format all of the html mark up except for <span style="text-decoration: underline;"> Underlined this Text </span>. If I use <u>Underlined this Text</u> it renders fine on the reports, but the control will not save it in that format. (yes I know <u> is depreciated...).
What I'm wanting to do is update the record after it is inserted and replace any <span style="text-decoration: underline;"> tags with <u> tags and their closing tags</span> with </u>. But I'm not sure how to do this in sql.
This is what a sample saved record currently looks like:
Plain Text <strong> Bold Text </strong><span style="color: #ff0000;"> Color Text </span>/<em><span style="text-decoration: underline;"> Underlined and Italics Text </span></em> and <span style="text-decoration: underline;"> Underlined Only Text </span>
This is what a sample saved record should look like:
Plain Text <strong> Bold Text </strong><span style="color: #ff0000;"> Color Text </span>/<em><u> Underlined and Italics Text </u></em> and <u> Underlined Only Text </u>
Any ideas as to how I can do this?
You can implement a custom content filter as shown below which will convert the span tag to U tag. Use the same approach for the other tags:
<telerik:radeditor runat="server"ID="RadEditor1" OnClientLoad="OnClientLoad" ContentFilters="MakeUrlsAbsolute,FixEnclosingP">
</telerik:radeditor>
<script type="text/javascript">
function OnClientLoad(editor, args) {
editor.get_filtersManager().add(new FixUnderline());
}
FixUnderline = function() {
FixUnderline.initializeBase(this);
this.IsDom = true;
this.Enabled = true;
this.Name = "FixUnderline";
this.Description = "This filter changes CSS underline to u tag";
};
FixUnderline.prototype = { _getElements: function(a, c) {
var b = a.getElementsByTagName(c);
if (!b) {
b = a.ownerDocument.getElementsByTagName(c);
} return b;
}, _replaceElementWithSpan: function(l, h, k) {
var m = this._getElements(l, h);
var d = [];
for (var b = m.length - 1; b >= 0; b--) {
Array.add(d, m[b]);
} for (var a = 0, c = d.length; a < c; a++) {
var e = l.ownerDocument.createElement("span");
e.style.cssText = k;
var f = d[a];
var g = f.innerHTML;
if ($telerik.isIE && g == " ") {
e.innerText = g;
} else {
Telerik.Web.UI.Editor.Utils.setElementInnerHtml(e, g);
} f.parentNode.replaceChild(e, f);
}
}, _replaceSpanWithElement: function(o, n, f) {
var q = this._getElements(o, "span");
var e = [];
for (var b = q.length - 1; b >= 0; b--) {
Array.add(e, q[b]);
} for (var a = 0, c = e.length; a < c; a++) {
var m = [];
var g = e[a];
for (var p = 0; p < g.childNodes.length; p++) {
Array.add(m, g.childNodes[p].cloneNode(true));
} if (g.style.cssText.toLowerCase() == f || g.style.cssText.toLowerCase() == (f + ";")) {
var h = o.ownerDocument.createElement(n);
for (var d = 0; d < m.length; d++) {
h.appendChild(m[d]);
} g.parentNode.replaceChild(h, g);
}
}
}, getHtmlContent: function(a) {
this._replaceSpanWithElement(a, "u", "text-decoration: underline");
return a;
}, getDesignContent: function(a) {
this._replaceElementWithSpan(a, "u", "text-decoration: underline");
return a;
}
};
FixUnderline.registerClass("FixUnderline", Telerik.Web.UI.Editor.Filter);
</script>
See more at https://www.telerik.com/forums/underline-in-radeditor-and-telerik-reporting and https://demos.telerik.com/aspnet-ajax/editor/examples/builtincontentfilters/defaultcs.aspx

Javascript how do sum class numeric value?

my question is class have number value how do fix this???
<span class="simpleCart_grandTotal"></span>
<p id="demo"></p>
<script>
var x = $('.simpleCart_grandTotal').text();
var y = 2;
var z = x * y;
document.getElementById("demo").innerHTML = z;
</script>
the output <span class="simpleCart_grandTotal"></span> is $10
this multiplied by 2 result will show <p id="demo"></p> is $20 and how do use this $ sign remove from first class and put second class??
var x = $('.simpleCart_grandTotal').text();
var intX = Number(x.replace(/[^0-9.]+/g,""));
var y = 2;
var z = "$" + (intX * y);
document.getElementById("demo").innerHTML = z;

How do I programmatically capture and replicate Ai path shape?

I'm using ExtendScript for scripting Adobe Illustrator. I was wondering if there was a sneaky way or a script available to programmatically capture and then replicate a path shape, sort of JavaScript's .toSource() equivalent.
Thanks
Try this:
main();
function main(){
var doc = app.activeDocument; // get the active doc
var coords = new Array(); // make a new array for the coords of the path
var directions = new Array();
var sel = doc.selection[0];// get first object in selection
if(sel == null) {
// check if something is slected
alert ("You need to sevlect a path");
return;
}
var points = sel.pathPoints;// isolate pathpoints
// loop points
for (var i = 0; i < points.length; i++) {
// this could be done in one lines
// just to see whats going on line like
//~ coords.push(new Array(points[i].anchor[0],points[i].anchor[1]));
var p = points[i]; // the point
var a = p.anchor; // his anchor
var px = a[0];// x
var py = a[1]; // y
var ldir = p.leftDirection;
var rdir = p.rightDirection;
directions.push(new Array(ldir,rdir));
coords.push(new Array(px,py));// push into new array of array
}
var new_path = doc.pathItems.add(); // add a new pathitem
new_path.setEntirePath(coords);// now build the path
// check if path was closed
if(sel.closed){
new_path.closed = true;
}
// set the left and right directions
for(var j = 0; j < new_path.pathPoints.length;j++){
new_path.pathPoints[j].leftDirection = directions[j][0];
new_path.pathPoints[j].rightDirection = directions[j][1];
}
}

How does one format a number with commas?

In Sencha Touch 2, how does one format a number with commas with either 0 or 2 decimal places? I imagine that there's a built in way to do this?
For example, I have 1234.567 and I need the number as 1,234 and 1,234.57.
You can do it easily with JavaScript only :
Add commas to the number
function addCommas(nStr)
{
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
Round to the nearest integer
var result = Math.round(original)
Round to two decimals
var result = Math.round(original*100)/100
Hope this helps
Take a look at this function from Ext JS 4's Ext.util.Format, which is not part of the Sencha Touch 2 API. Just implement it in your application and you're set.
http://docs.sencha.com/ext-js/4-1/source/Format.html#Ext-util-Format-method-number
function iFormatValueTwoDecimals(inValue)
{
var leftSide = Math.floor(inValue);
var rightSide = Math.round((inValue - leftSide)*100);
if(rightSide === 0)
{
rightSide = '00';
}
else if(rightSide < 10)
{
rightSide = rightSide + '0';
}
return leftSide+'.'+rightSide;
}