have a chess board in TABLE, each square a TD.
how would one use html5 gradients (and javascript for randomness) to create a wood texture background for the dark squares?
I'm grabbing a big wood texture (change to one you like) and grabbing a random piece of it at 50% opacity, then underneath is a random brownish color to add a unique undertone to each square. You can adjust all these to get an effect you want. I messed with some gradients and they looked silly.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
<style>
div {
width: 100px; height: 100px; margin: 1px;
}
div.texture {
background: url(http://gallery.hd.org/_exhibits/textures/wood-grain-closeup-1-DHD.jpg);
opacity:0.4; filter:alpha(opacity=100);
}
</style>
<script>
$(function(){
$('div.bg').each(function(){
// make each square a random brown
var browns = new Array('CD853F','8B4513','A0522D');
var col = Math.floor(Math.random()*3);
$(this).css('background-color',browns[col]);
// the dimensions of your texture minus square size
var image_width = 500;
var image_height = 400;
// get a random positions
var x = Math.floor(Math.random()*image_width);
var y = Math.floor(Math.random()*image_height);
// make them negative
x = x - (x * 2);
y = y - (y * 2);
var d = $(this).children('div.texture');
d.css('background-position', x+'px'+' '+y+'px');
});
});
</script>
<div class='bg'><div class='texture'></div>
<div class='bg'><div class='texture'></div>
Related
I have looked through a lot of answers for similar problems but was not able to find any that actually seem to work for me. My problem is very simple, I have two images occupying the exact same spot, however they are at different angles from each other. When I zoom in or out, the images should still occupy the same spot as each other, however, the rotated image always moves.
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<style>
.building {
width:40px;
height:40px;
position:absolute;
left:100px;
top:100px;
}
</style>
<div style="display:flex;justifty-content:space-evenly;width:80%;">
<div id="container" style="position:relative;width:200px;height:200px;border:1px solid red;">
<img src="image1.png" class="building">
<img src="image2.png" class="building" style="transform:rotate(45deg);">
</div>
<div>Scale:<input id="scale" type="text" onChange="changeScale(this.value)"></div>
</div>
<script>
var oldScale = 1;
function changeScale(_scale) {
var zoom = _scale;
var zdelta = (_scale/oldScale);
oldScale=_scale;
var w = $("#container").width();
var h = $("#container").height();
$("#container").width(w*zdelta);
$("#container").height(h*zdelta);
$(".building").each(function() {
var p = $(this).position();
w = $(this).width()*zdelta;
h = $(this).height()*zdelta;
var r = getCurrentRotation(this);
$(this).css({top: (p.top*zdelta), left: (p.left*zdelta)});
$(this).width(w);
$(this).height(h);
});
}
</script>
I have tried to use the following to adjust the rotated image:
$(this).css({top: (p.top*zdelta)+( (h/2)*Math.cos(r) ), left: (p.left*zdelta)+( (w/2)*Math.sin(r) ) });
as well as
function GetArrowLeftShift(w,degrees )
{
var result = (w / 2) * Math.cos( degrees * Math.PI / 180 );
return result;
}
as well as Math.sqrt( Math.pow(w/2,2)+Math.pow(h/2,2) );
Note: I have tried to make the problem easier by using the center position of the square with equal sized images. The real application with have the images in any position, and their sizes will vary but they will need to retain their exact positions in relation to the other images in the square.
So, since I could never find a good mathematical solution to solve this problem, I have ended up doing the following:
var r = getCurrentRotation(this);
$(this).css({transform:"rotate(0deg)"});
var p = $(this).position();
var w = $(this).width()*zdelta;
var h = $(this).height()*zdelta;
$(this).css({top:p.top*zdelta,left:p.left*zdelta,transform:"rotate(+"r+"deg)"});
In essence, I set the rotation to 0, then scale the image and reposition the image and set its rotation back to its original angle.
Can anyone please help me identify why this code isn't working? It's copied directly from a youtube tutorial so I can't see where I've gone wrong!
Thank you!
I basically want to create matrix rain, but my own version of symbols within it. Would someone be able to correctly identify what's wrong with this code? I have tried to input it into http://phptester.net/
<DOCTYPE html>
<html>
<head>
<title>Matrix Rain</title>
<style>
/*basic resets */
* {margin:0; padding: 0;}
/* adding a black bg to the background to make things clearer */
body {background: black;}
canvas {display: block;}
</style>
</head>
<body>
<canvas id="c"></canvas>
<script type="text/javascript">
var c = document.getElementbyId("c");
var ctx = c.getCont("2d");
//making the canvas full screen
c.height = window.innerHeight;
c.width = windows.innerWidth;
var ganoti = "诶比西迪艾弗艾尺艾勒艾娜吉吾艾儿"
//converting the string into an array of single characters
chinese = chinese.split ("");
var font_size = 10;
var columns = c.width/font_size; //number of columns for rain
//an array of drops - one for column
var drops = [];
//x below is the x coordinate
//1 = y co-ordinate of the drop(same for every drop initially)
for (var = x; x < columns; x++)
drops[x] = 1;
//drawing the characters
function draw () {
//Black BG for the canvas
//translucent BG to show trail
ctx.fillStyle - "rgba(0,0,0,0.05)";
ctx.fillRect(0,0, c.wdith, c.height);
ctx.fillstyle = "DB7093";
ctx.font = font_size + "px arial";
//looping over drops
for (var i = 0; i < drops.length; i++) {
var text = ganoti[Math.floor(Math.random()*ganoti.lenth)];
//x = i*font_size, y = value of drops[i]*font_size
ctx.fillText(text, i*font_size, drops[i]*font_size);
//sending the drop back to the top randomly after it has crossed
screen // it should be in comment line
//adding a randomness to the reset to make the drops scattered on the
Y axis // it should be in comment line
if (drops[i]*font_size > c.height && Math.random() > 0.975)
drops[i] = 0;
//incrementing Y coordinate
drops[i]++
}
}
setInterval(draw, 33);
</script>
</body>
</html>
}
}
phptester.net requires PHP, your code is HTML, besides has redundant double } in the end.
I am creating a game for coursework, two tanks placed on a canvas with input boxes for the initial velocity and angle of the turret, then a button to fire a projectile (currently a div element in the shape of a circle), which calls a function in this case it is fire1. I have messed around for a few days and can't seem to get it to work, "bullet" is my div element.
function fire1 () {
var canvas = document.getElementById("canvas")
var bullet = document.getElementById("bullet");
bullet.style.visibility = "visible"
var start = null;
var intialVelocity = velocity1.value
var angle = angle1.value
var g = 9.81;
var progress, x, y;
function step(timestamp) {
if(start === null) start = timestamp;
progress = (timestamp - start)/1000;
x = (turret1.x + 80) + (intialVelocity*progress)
y = (turret1.y - 400) + (intialVelocity*progress)*Math.sin(angle*toRadians) - (0.5*g*(progress^2));//)
bullet.style.left = x + "px";
bullet.style.bottom = y + "px";
requestAnimationFrame(step);
}
requestAnimationFrame(step);
}
Below is my css bit of my bullet.
#bullet {
position: absolute;
left: 0;
bottom: 50%;
width: 1em;
height: 1em;
border-radius: 0.5em;
background: red;
visibility: hidden;
}
I am very new to javascript, css and html so help would be very appriciated, I'm trying to incorporate the trajectory formula will this work? I also want it to be animated so it follows a path when fired. Thanks
I fixed this a long time ago but forgot to update with solution, below is how x and y are calculated for the trajectory:
x = ((turret.anchorX + negative*(initialVelocity*progress*Math.cos(angle*toRadians)))); //x-coordinate for bullet calculated by using x=ut.
y = ((720 - turret.anchorY + (initialVelocity*progress*Math.sin(angle*toRadians)) + (0.5*g*(Math.pow(progress,2))))); //y-coordinate for bullet calculated by usnig ut+0.5at^2.
$(this).css({
position: 'absolute',
left: Math.random() * ($('.parentcontainer').width() - $(this).width()),
top: Math.random() * ($('.parentcontainer').height() - $(this).height())
});
I got this each loop that will randomly place elements within a div. The problem with this is that the elements will overlap each other sometimes because they are absolute positioned. Is there anyway to go around this in js to check position? or maybe rewrite this with margin values? Thanks a ton!
There's a few different ways you can do to achieve this. I find it easiest to try to define the problem in one sentence:
New square's position must be at least X distance from current square positions
Using this sentence, we can make some simple theories as to how the code will work.
Assuming all squares are 50x50 pixels, we can write some checks.
Here are some pseudo code steps we could follow:
Generate a random position for newSquare
Compare the x and y positions of newSquare to all existing squares
If either of the x and y positions of newSquare are further away from the other squares, newSquare can be placed
Otherwise, try again
var container = $('#container');
var squareSize = 50;
var containerSize = 500;
for (var i = 0; i < 20; i++) {
var foundSpace = false;
while (!foundSpace) {
// Generate random X and Y
var randX = Math.floor(Math.random() * (containerSize - squareSize));
var randY = Math.floor(Math.random() * (containerSize - squareSize));
var hitsSquare = false;
var squares = container.children();
squares.each(function(index, square) {
var square = $(square);
// parseInt() because .css() returns a string
var left = parseInt(square.css('left'));
var top = parseInt(square.css('top'));
// Check boundaries
var hitsSquareX = Math.abs(left - randX) < squareSize;
var hitsSquareY = Math.abs(top - randY) < squareSize;
// Will overlap a square
if (hitsSquareX && hitsSquareY) {
hitsSquare = true;
// jQuery break .each()
return false;
}
});
// If doesn't overlap any square
if (!hitsSquare) {
foundSpace = true;
var newSquare = $('<div class="square">');
newSquare.offset({
left: randX,
top: randY
});
container.append(newSquare);
}
}
}
#container {
position: relative;
}
.square {
position: absolute;
background-color: red;
width: 48px;
/* border adds 2px */
height: 48px;
/* border adds 2px */
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
</div>
You should look for collision detection.
In my opinion this is a great tutorial: https://www.youtube.com/watch?v=XYzA_kPWyJ8, but there are several other great ones out there.
Good luck :)
I've just gotten started with coding on my new school, and I'm making a website as my first project. Now I'm trying to make this "matrix" animation appear as my background (made mostly in javascript), but I just cant get it to work. Could anyone help me with this issue? If it's even possible, that is. Thanks. (I know the code is kind of messy... first time posting, no idea how to do this)
HTML
<div class="inhoud">
<div id="container">
<div id="over_stuff">
Here's some stuff over #pixie!
</div>
<canvas id="c"></canvas>
</div>
</div>
CSS
/*basic reset*/
#container {
overflow: hidden;
position: relative;
z-index: 1;
}
#c {
z-index: 0;
background: #010222;
background:
}
#over_stuff {
color: white;
position: absolute;
top: 0px;
left: 0px;
z-index: 5;
padding: 10px;
}
* {
margin: 0;
padding: 0;
}
/*adding a black bg to the body to make things clearer*/
body {
background: black;
}
canvas {
display: block;
}
Javascript
var c = document.getElementById("c");
var ctx = c.getContext("2d");
//making the canvas full screen
c.height = window.innerHeight;
c.width = window.innerWidth;
//chinese characters - taken from the unicode charset
var chinese = "dirk";
//converting the string into an array of single characters
chinese = chinese.split("");
var font_size = 10;
var columns = c.width / font_size; //number of columns for the rain
//an array of drops - one per column
var drops = [];
//x below is the x coordinate
//1 = y co-ordinate of the drop(same for every drop initially)
for (var x = 0; x < columns; x++)
drops[x] = 1;
//drawing the characters
function draw() {
//Black BG for the canvas
//translucent BG to show trail
ctx.fillStyle = "rgba(0, 0, 0, 0.05)";
ctx.fillRect(0, 0, c.width, c.height);
ctx.fillStyle = "#555"; //green text
ctx.font = font_size + "px arial";
//looping over drops
for (var i = 0; i < drops.length; i++) {
//a random chinese character to print
var text = chinese[Math.floor(Math.random() * chinese.length)];
//x = i*font_size, y = value of drops[i]*font_size
ctx.fillText(text, i * font_size, drops[i] * font_size);
//sending the drop back to the top randomly after it has crossed the screen
//adding a randomness to the reset to make the drops scattered on the Y axis
if (drops[i] * font_size > c.height && Math.random() > 0.975)
drops[i] = 0;
//incrementing Y coordinate
drops[i]++;
}
}
setInterval(draw, 33);
I FIXED IT! all it took was to add
Postion:fixed to the code, and then give that a z-index of -10. thanks for taking the time to look at my code anyway ;)