Random section of image to display - image

I have a large image roughly 2500px wide and 300px tall. I have saved 4 versions of this image:
left side
right side
left flipped
right flipped
Images are saved as follows.
img/Banner1.png
img/Banner2.png
img/Banner3.png
img/Banner4.png
var Num;
function gen(){
var x;
x = Math.floor(Math.random()*4+1);
Num = x;
while(Num == x){
Num = Math.floor(Math.random() * 3 + 1);
}
document.write("<style type='text/css'>");
document.write(".banner{");
document.write("background-image: url('img/Banner" + Num + ".png');");
document.write("background-position:left top, left bottom;");
document.write("background-repeat:repeat-x;");
document.write("-webkit-border-top-left-radius: 50px;");
document.write("-webkit-border-top-right-radius: 50px;");
document.write("-webkit-box-shadow: 0px 0px 5px 5px rgba(10, 10, 10, 0.5);");
document.write("-moz-box-shadow: 0px 0px 5px 5px rgba(10, 10, 10, 0.5);");
document.write("box-shadow: 0px 0px 5px 5px rgba(10, 10, 10, 0.5);");
document.write("border-bottom: 3px solid #505050;");
document.write("</style>");
}
gen();
Thus far you may have noticed there is no problem however this seems somewhat in-efficient. I am wondering if anyone has a recommendation for improvement in regards to performance.

Your code does not use Banner4.png. That's because Math.random() does exclude the value 1, so Math.random() is in [0,1), triple that and you are in [0,3), add one to get [1,4) and take the floor and you get {1,2,3}. So make that * 4 instead of * 3.
As Kevin already mentioned in a comment, your use of x seems unmotivated and bad performance-wise. You don't seem to be using its value, so you might as well avoid the loop and write
Num = Math.floor(Math.random() * 4 + 1);
as the first line of the function. You might also combine all those document.write calls into a single one, for a slight performance gain, but I'd guess the effect should be rather negligible. The effect by omitting the loop would be negligible as well, but there it would also make the code less confusing, which is a reason why I'd suggest that modification in any case. Otherwise, don't try to optimize for performance if it reduces maintainability, unless you know for sure that you have a reason to need optimization.

Related

How could one find the line by line instructions that built-in commands follow when executing

I'm playing around with vertices and beginShape() to get a feel for it and understand how the function works. I used the p5.js website's reference page as a guide.
While doing that, I wrote this code which gave me an unexpected result:
stroke('red')
strokeWeight(5)
beginShape(POINTS);
vertex(25, 25); // top left
stroke('yellow')
vertex(25, 75); // top right
push()
stroke('green')
strokeWeight(25)
vertex(75, 75); // bottom right
pop()
push()
strokeWeight(25)
vertex(75, 25); // bottom left
pop()
endShape();
RESULT I EXPECTED:
top left: color = red, size = 5
top right: color = yellow, size = 5
bottom right: color = green, size = 25
bottom left: color = yellow, size = 25
RESULT I GOT:
top left: color = red, size = 5
top right: color = yellow, size = 5
bottom right: color = green, size = 5
bottom left: color = red, size = 5
SUMMARY:
I couldn't figure out why I got the result I got, and playing with the code made the results even more confusing, I came to a conclusion that I just don't know what happens within the computer when
beginShape() is called.
I'm looking for an in depth explanation for what the computer does when beginShape() is called. (By explanation I mean like the way Dan Shiffman from the YouTube channel "Coding Train" explains that when mousePressed() is called the computer stops running draw() and executes the mousePressed() function. Another example would be: Dan Shiffman also explains that first of all setup() runs first when executing the program and only then draw() loops.
QUESTIONS:
I want to understand what happens within the program when I call beginShape(). This kind of information is not given in the p5 reference page. (a different phrasing for the question could be: how does the program execute the function beginShape().)
I want to know what this type of 'behind the scenes' information about the language/library is called so I could find it more easily in the future, for p5 and for other languages too.

Mix two non-opaque colors with "hue" blend mode

I want to implement color blending as described in the W3C compositing and blending spec. (I'm doing this in JavaScript but the language shouldn't really matter for solving my problem.)
In retrospect: During the implementation of the answer to this question I realized that this would probably make for a pretty nice standalone package. In case you're interested you can grab it from npm.
It worked out pretty well so far but I wanted to take these algorithms a step further and add support for alpha channels. Thanks to the SVG compositing spec providing all the needed formulas that wasn't too hard.
But now I'm stuck with implementing the blend modes that the W3C spec describes as non-separable which are (as known from Photoshop): hue, saturation, color and luminosity.
Sadly, algorithms for those aren't available in the SVG spec and I have no idea how to work with those. I guess there are a modified versions of the formulas provided by the W3C for working with alpha channels which I'm missing.
To make my problem a little more visual I'll show what Photoshop gives me for hue blending two colors:
This is what I'm also able to reproduce with the non-alpha algorithm from the mentioned W3C spec.
What I can't reproduce is the result that Photoshop gives me when I put a lower alpha on both the source and the backdrop color:
Does anyone know how to achieve that result programmatically?
Update 1: Changed illustrations (adding HSVA and RGBA codes) to clarify the used colors.
Update 2: To check possible solutions I'll attach two other Photoshop-generated blending examples:
Update 3: So it turned out that in addition to not having a clue about color blending I also messed up my Photoshop settings, making the task to solve my question even harder. Fixed the example images for possible future passerbies.
The Hue alpha you have at your second image does not represent the alpha color composition formula, but it rather reflects the Porter Duff alpha composition Source Over as defined here 9.1.4. Source Over and it uses the following formula:
If you want to achieve that kind of blending, which is not proper Hue blending, you can use the following formula in javascript:
PDso = { // Ported Duff Source Over
r: ((S.r * S.a) + (B.r * B.a) * (1 - S.a)) / aR,
g: ((S.g * S.a) + (B.g * B.a) * (1 - S.a)) / aR,
b: ((S.b * S.a) + (B.b * B.a) * (1 - S.a)) / aR,
};
// where
// S : the source rgba
// B : the backdrop rgba
// aR : the union alpha (as + ab * (1 - as))
Hue Blending Mode with Alpha Channel
Here is a screenshot of the exact hue blend source over backdrop using the alpha color composition formula that I have created in Photoshop:
The middle square with the green highlighted letters is the correct blend representation. Here is the CSS Hue mix blend with the source color inside the backdrop color, using the new CSS mix-blend-mode (run the code snippet):
.blends div {
width:140px;
height:140px;
}
.source {
mix-blend-mode: hue;
}
.backdrop.alpha {
background-color: rgba(141, 214, 214, .6);
isolation: isolate;
}
.source.alpha {
background-color: rgba(255, 213, 0, .6);
}
<div id="main">
<div class="blends alpha">
<div class="backdrop alpha">
<div class="source alpha"></div>
</div>
</div>
</div>
If you use a color picker, you'll get almost the same values (211, 214, 140 <> 210, 214, 140). That can be due to slightly different algorithms, or different rounding methods, but it doesn't really matter. The fact is, that this is the correct result when blending alpha colors with hue blend mode.
So, now we need the formula to have the proper color values for the alpha color composition applied to our hue blend mode. I have searched a little bit and I found everything inside the Adobe Document management - Portable document format - Part 1: PDF 1.7. We can find the color composition formula at the page 328 after the Blend Modes:
11.3.6 Interpretation of Alpha
The colour compositing formula
This is the formula I managed to get the proper and closer to Photoshop match for the Hue Blending Mode with alpha channel. I wrote it like this in javascript:
function Union(ab, as) {
return as + ab * (1 - as);
}
function colourCompositingFormula(as, ab, ar, Cs, Cb, Bbs) {
return (1 - (as / ar)) * Cb + (as / ar) * Math.floor((1 - ab) * Cs + ab * Bbs);
}
var aR = Union(B.a, S.a); // αr = Union(αb, αs) // Adobe PDF Format Part 1 - page 331
var Ca = {
// Adobe PDF Format Part 1 - page 328
r: colourCompositingFormula(S.a, B.a, aR, S.r, B.r, C.r),
g: colourCompositingFormula(S.a, B.a, aR, S.g, B.g, C.g),
b: colourCompositingFormula(S.a, B.a, aR, S.b, B.b, C.b)
};
// where
// C : the hue blend mode result rgb
// S : the source rgba
// B : the backdrop rgba
// aR : the union alpha (as + ab * (1 - as))
// Ca : the final result
body {
padding:0;
margin:0;
}
iframe {
width: 100%;
height: 200px;
border:0;
padding:0;
margin:0;
}
<iframe src="https://zikro.gr/dbg/html/blending-modes/"></iframe>
My test example can be found here. At the 2.5 With Alpha (Hue Blending Algorithm Computed), you cay see the final hue blend mode result with alpha. It has slightly different values than Photoshop result but I got the exact same result (202, 205, 118) in Fireworks, hue blending the source and backdrop colors:
All applications have their own slightly different algorithms and maybe the formula I have used is rather old and maybe there is a newest version.
Starting from here
Hue blending creates a color with the hue of the source color and the saturation and luminosity of the backdrop color.
I can come up with some formulas, but they might be rubbish, although they completely reproduce the original numbers posted:
h: hSource + deltaH * (1 - aSrouce) * aBackdrop * 0.41666666 = 50; 63
s: sBackdrop * 0.9 + deltaS * (1 - aBackdrop) * aSource * 0.20833333 = 45; 47.5
l: lBackdrop * 0.957142857 + deltaL * (1 - aBackdrop) * aSource * 0.77 = 67; 63.3
a: 1 - (1 - aSource)^2 matches always

Algorithm for smooth alpha crossfade?

My application fades between various media and text layers by adjusting their alpha values. However, when using a linear crossfade the brightness appears to "dip" halfway through and then fade back up. After some searching I found this answer that explains the issue, however the suggested solution, fading only one layer at a time, won't work for me since most of the layers I use already contain transparency.
Here's an example of the issue I'm having, in HTML/CSS (code below because SO requires it.
<style>
body, html {
width: 100%;
height: 100%;
margin: 0;
background-color: black;
}
.example {
position: absolute;
width: 100%;
height: 100%;
opacity: 0;
}
#example1 {
background-color: red;
animation: 1s linear 0s fade infinite alternate;
}
#example2 {
background-color: red;
animation: 1s linear 1s fade infinite alternate;
}
#keyframes fade {
from {opacity: 0;}
to {opacity: 1;}
}
</style>
<div id="example1" class="example"></div>
<div id="example2" class="example"></div>
The two divs should fade their opacities back in forth, resulting in a solid red image the entire time. Instead, it appears to dip in brightness.
What is the algorithm or formula for creating a smooth crossfade using alpha? I'm using OpenGL, if that's relevant. (The HTML/CSS snippet was just the easiest way of demonstrating the issue).
Sorry, but it's not possible.
First off, the equation you want is defined here. I'll copy it here in other terms:
outputColor = overAlpha * overColor + (1 - overAlpha) * underColor
If I understand your question correctly, you're looking for a periodic function f(t) for your alpha transition such that:
1 = f(t - 1) + (1 - f(t)) * f(t - 1) = f(t - 1) + f(t) - f(t - 1) * f(t)
The only function that satisfies that equation, at least according to wolfram alpha is the constant 1. And that won't work if you want it to be zero at the beginning, and have it loop infinitely.
Unless you don't want a periodic function, and you just want your fades to look kinda nice. The equation linked above.
There is some good discussion of this topic at this other question.
It's true that there is no perfect solution, other than a step function, but you can mitigate the effects somewhat. The important thing is to have easing functions that cross at a relatively "high" point, rather than at 0.5. See graphs at this answer.

css: max-width or max-size

On my website I have images that have a width of 720 pixels. I want images to be either 95% of the width of the page (max-width: 95%), or 720px if the page width is too large. So, on larger screens, I don't want to blow them up. Is there a way to achieve this in CSS?
You can specify a width and a max-width. The element will respect the width, or shrink if max-width is smaller.
This behavior is described on the MDN.
It prevents the used value of the width property from becoming larger
than the value specified for max-width.
img {
background-color: grey;
border: 1px solid blue;
width: 400px; /* Ideal width */
max-width: 80%; /* Max width */
}
<img src="http://placehold.it/400x400">
Note as indicated in the other answer, you could set width to a percentage and max-width to pixels. The end result is the same: the calculated width of the image becomes the smallest of the two.
To me, specifying the width in pixels makes more sense semantically. After all, you want to have an image of so many pixels wide (related to the image itself), and want to have a constraint on the percentage for smaller screens. But if you write it the other way around, I won't hold it against you. ;-)
GolezTrol has it nearly right but just has the max-width and width values the wrong way round.
Below is the correct elements set as you require.
img {
width: 95%; /* Ideal width */
max-width: 720px; /* Max width */
}
<img src="http://placehold.it/800x800">
Max-width MDN Documentation
Width MDN Documentation

Retrieving SVG position in Firefox vs Chrome with viewBox set

I'm trying to get the left position of an svg element that has a viewBox set. The viewBox is basically a square, while the actual svg element is more rectangular. In most cases, this isn't a problem (and in Chrome, everything works fine), however, when trying to get the left position of the element within Firefox, as the viewBox is square, Firefox reports the left position of the viewBox rather than the svg element.
See http://jsfiddle.net/c6SW6/11/ for an example which should make things obvious.
Basically, in Chrome, the number reported for the left position is 8 This is the number that I want. In Firefox, it's reported as 108. How do I get Firefox to report the number 8 as well?
Code
HTML
<div>
<svg viewBox="0 0 100 100"><rect x=0 y=0 width=100 height=100></rect></svg>
</div>
<p>
</p>
CSS
div {
height: 100px;
width: 300px;
margin: 0;
padding: 0;
position: absolute;
top: 200px;
}
svg {
background-color: green;
height: 100%;
width: 100%;
}
JS
$('p').text($('svg').offset().left);
Assuming we gave the rect an id="r" attribute...
If you just want the offset of the rect in the svg itself then it's
$('p').text(document.getElementById("r").getCTM().e);
If you want the offset from the page origin instead...
Call rect.getBoundingClientRect() and the left and top will contain the answer
$('p').text(document.getElementById("r").getBoundingClientRect().left);
or alternatively rect.getScreenCTM() the e and f members of the result will be the answer
$('p').text(document.getElementById("r").getScreenCTM().e);
If you rotate the svg using a transform then you'll get different answers, getBoundingClientRect() will give you an unrotated bounding rect but getScreenCTM will give you a transformed offset, since you're not doing that you can use either currently.
The 8 is the difference, i.e. the position of the element element on the page. That's not consistent with the description but if you want 8 then it's:
$('p').text(document.getElementById("r").getScreenCTM().e -
document.getElementById("r").getCTM().e);

Resources