Scenerio:
Here is my HTML.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id='textbox'>
<div id="sibling"></div>
<div id='canvasParent'>
<canvas id="myCanvas" style="border:1px solid #000000">
</canvas>
</div>
</div>
</body>
</html>
Here is the js for the same.
function outputsize() {
console.log(canvasParent)
canvasParent.clientWidth = textbox.clientWidth - sibling.clientWidth;
myCanvas.width = canvasParent.clientWidth;
}
outputsize();
new ResizeObserver(outputsize).observe(textbox);
Here is the css for the same.
#textbox {
display:flex;
flex-direction:row;
border:1px solid green;
flex:1;
}
#sibling {
width:200px;
border:4px solid yellow;
}
#canvasParent {
border:2px solid red;
flex:1;
}
First div have 2 child divs namely sibling and canvasParent, sibling has fixed width, whereas canvasParent will take the remaining width.
Currently it is not behaving as expected once we decrease window width then the first div(sibling) is decreasing its width, and the width of other div is constant, whereas I want sibling div to remain at its fixed width once user decrease the window width and the other div to shrink/grow.
Here is the JSBIN for the same having css, js for the described scenerio.
Expectation:
Once we resize the window, the first div (sibling) will keep its fixed width and the other div having canvas inside it will shrink/grow based on window resizing, and the canvas inside it will take its parent width(logic for canvas to take its parent div is already written in js file.)
And resizing works fine, once I remove the canvas element from the second div.
Here is the JSBIN that show first div to remain at fixed width and other div to shrink/grow once user reduces windows width.
You can use CSS Grid to get the effect:
#textbox {
display: grid;
grid-template-columns: 200px 1fr;
grid-template-rows: 1fr;
grid-template-areas: ". .";
grid-column-gap: 10px;
}
#sibling {
background: red;
}
#canvasParent {
background: black;
}
#myCanvas {
width: 100%;
background: pink;
}
<div id="textbox">
<div id="sibling"></div>
<div id="canvasParent">
<canvas id="myCanvas"></canvas>
</div>
</div>
You can design CSS Grids here: https://grid.layoutit.com/?id=2NkZ4JY
Related
So i'm making a online profile for myself, and i was doing research on how to make the landing page as attractive as possible. Came across HTML5 Canvas, did some research and experimentation on making an interactive background, and this is the result (in codepen).
Now, i would like to write my name in the middle of the canvas with the text infront of the animation.
The animation gets called in a recursive loop and the initialization function gets called when the page is resized or refreshed.
Problem is, i can't get the text infront, and for some odd reason, my text shrinks when the page is resized.
Here is my pen
https://codepen.io/hamza-tariq-khan/pen/mzKMNd
<!doctype html5>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Canvas Resize</title>
<style>
canvas{
/* border: 1.5px solid black; */
/* background-color: red; */
display: block;
}
body{
margin: 0;
overflow-x: hidden;
}
.wrapper{
margin : 20px;
}
</style>
</head>
<body>
<canvas></canvas>
<script src="script.js"></script>
<div class="wrapper">
<h1>This is a sample sentence</h1>
</div>
</body>
</html>
and this is an idea of what i want to achieve. (the writing of Mathew williams infront of the moving background)
http://findmatthew.com
If I were you, I would skip rendering text in the canvas and apply some CSS rules to the div to make it show up on top instead. The CSS approach improves performance and also improves accessibility. A blind person using a screen reader will be able to figure out what's in the div, but reading text in the canvas is not supported. Most importantly, it's a lot easier to reason about HTML and CSS than it is to reason about text rendering in a canvas element. How often will you do that? When you come back to change the code in a month, six months, or a year, it will be easier to change the HTML/CSS than it will be to think through the canvas rendering logic all over again!
To do this, use the following CSS for div wrapper:
.wrapper{
position: absolute;
top: 0px;
color: red;
font-size: 60px;
margin : 20px;
}
position: absolute allows the div to overlap the canvas, and properties top, bottom, left and right allow you to control how far the div is offset from the edges of its parent (in this case wrapper is a child of body). I adjusted color and font-size just to make it obvious that it works.
And here is a link to a working example: https://codepen.io/anon/pen/LqoroR
var canvas = document.getElementById("dm_graphs");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "blue";
ctx.fillRect(0, 0, canvas.width, canvas.height);
<style>
body,
canvas,
html {
font: 24px sans-serif;
height: 100hv;
width: 100%;
margin: 0;
padding: 0;
background: #888;
color: #135;
overflow: hidden
}
#fs {
position: relative
}
#txt,
canvas {
position: absolute
}
#txt {
color: yellow;
margin: 50px;
font: 24px subpixel-antialiased Noto Sans Samaritan;
background: transparent
}
</style>
<body>
<div id="fs">
<canvas id="dm_graphs" width="400" height="300"></canvas>
<div id="txt">This is a text over a canvas.</div>
</div>
</body>
I'm trying to build a 3D viewer with three.js, that has full height but leaves space for a side panel. The vertical layout works as expected, but as soon as I append the render's dom element, a horizontal scroll bar appears.
Attached is a minimal working example. I would expect to just see the (black) canvas element and the red body. But after v.append(renderer.domElement), the page gets larger (filled with blue, html element) and a horizontal scroll bar appears. It seems the page is larger than its body.
See https://jsfiddle.net/5jnvt4jh.
Has anybody an idea, what may be happening there? I couldn't find any margin or padding with Chrome and Firefox. Thanks :).
MWE
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<style>
html {
background-color: blue;
}
body {
margin: 0px;
height: 100vh;
background-color: red;
}
#viewer {
height: 100%;
width: 80vw;
background-color: green;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/86/three.min.js"></script>
</head>
<body>
<div id="viewer"></div>
<script>
var v = document.getElementById('viewer');
var renderer = new THREE.WebGLRenderer();
v.append(renderer.domElement);
renderer.setSize(v.clientWidth, v.clientHeight);
</script>
</body>
</html>
Change style of body to:
body {
margin: 0px;
height: 100vh;
background-color: red;
overflow:hidden;
}
jsfiddle: https://jsfiddle.net/raushankumar0717/5jnvt4jh/2/
Whenever the html page is of same dimension as the screen view scroll bar is not needed, as u go on adding the elements in the webpage(refer my code) and length of the page is greater than the view scroll bar appears and changes according to the web length increases.I have already seen how to find the scroll position using scrolltop() etc .but never understood how it appears.
simple dummy code
<html>
<head>
<style>
.box1{
height:50%;
width:100%;
border:1px solid black;
}
.box2{
height:50%;
width:100%;
border :1px solid black;
}
</style>
</head>
<body>
<div class="box1">
</div>
<div class="box2">
</div>
</body>
</html>
page length increased
<html>
<head>
<style>
.box1{
height:50%;
width:100%;
border:1px solid black;
}
.box2{
height:50%;
width:100%;
border :1px solid black;
}
.box3{
height:50%;
width:100%;
border:1px solid black;
}
</style>
</head>
<body>
<div class="box1">
</div>
<div class="box2">
</div>
<div class="box3">
</div>
</body>
</html>
I am curious of how the browser calculates scroll bar length(in %) according to the length of the page and how the scroll bar length increases/decreases based on the webpage .
Is there any mathematical formula (not sure)something like
calculate the whole length of page (length of elements) - (view dimension of the browser)/(by some unknown variable).
The browser takes the height of your screen (say 1000px), and calculates the percentage of the total document height. If the document height is 3000px, then the result is ~33%. Therefore the height of the scrollbar must be 33% of your screen height, which in pixels is 333px.
It may be something like this
var heightIs = parseInt( $(window).height() / ( $("body").height() / $(window).height() ) );
You get the ratio bewteen body height and window height and then divide the window height with that ratio.
I've got a page that is supposed to display a line graph. There is a title at the top, the graph in the middle, then a table below them. It is laid out roughly like this:
<div>
<h1>Title</h1>
</div>
<div>
<canvas1></canvas>
<canvas2></canvas>
</div>
<div>
<table></table>
</div>
Right now each of the 'div' blocks are staying separate from each other, which is good. However, the two canvas's, despite having different z-index values, are next to each other instead of stacking on top. I've read that their position values should both be set to absolute, but whenever I do this, the table immediately moves on top of the canvas.
What position and display values do I need to set to the div's and the elements inside them to get the canvasses on top of each other (both are the same dimensions) without anything else stacking on top of their div?
Edit: Here's a fiddle
HTML:
Wrap the 2 canvases inside a wrapper div.
<div id="wrapper">
<canvas id="canvasBottom" width=300 height=200></canvas>
<canvas id="canvasTop" width=300 height=200></canvas>
</div>
CSS:
Position the 2 canvases at the same top & left relative to the wrapper div.
#wrapper{
position:relative;
width:300px;
height:200px;
}
#canvasTop,#canvasBottom{
position:absolute; top:0px; left:0px;
width:300px;
height:200px;
}
If you set the "position" attribute of your two canvas to "absolute", then the two canvas would stick to the parent element, say, your div.
The reason why the table moves on top of your canvas is that the table, in a sense, "neglected" your canvas and was located as if there is no canvas.
What you should do is keep the absolute position value of the canvas and set the "top" value of table to the height of your canvas, then the table would be just beneath the canvas.
let me give you an example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Animal World</title>
<style>
canvas {
width: 50%;
height: 100px;
position: absolute;
}
#dog {
z-index: 1;
}
#cat {
z-index: 0;
background-color: blue;
}
table{
background-color: rgb(196, 255, 216);
position: relative;
top: 100px;
border-collapse: collapse;
}
caption{
font-weight: bolder;
font-size: 2em;
background-color: rgb(196, 255, 216);
border: 1px groove lightblue;
}
td,th{
border: 1px groove lightblue;
}
</style>
</head>
<body>
<div>
<h1>Animal</h1>
</div>
<div id = "canvas">
<canvas id = "dog"></canvas>
<canvas id = "cat"></canvas>
</div>
<script>
var dog = document.getElementById('dog');
var cat = document.getElementById('cat');
var dog_ctx = dog.getContext('2d');
var cat_ctx = dog.getContext('2d');
dog_ctx.fillStyle = "red";
dog_ctx.fillRect(20, 20, 20, 20);
</script>
<div class = "table">
<table>
<caption>Animal</caption>
<tr>
<th>Dog</th>
<th>Panda</th>
<th>Cat</th>
</tr>
<tr>
<td>middle</td>
<td>large</td>
<td>small</td>
</tr>
</table>
</div>
</body>
</html>
Definitely use flex-box to float stacked canvases to perfection. Note: you'll need to use "display: flex" for the entire layout including HTML and BODY elements, plus any parent containers/ divs.
#canvasWrapper {
align-items: center;
display: flex;
height: 100%;
justify-content: center;
}
canvas {
position: absolute;
}
I am trying to add a border to a div element when the moust is hovering on the div, but I found after the border is aded, the boder will occupy some space and make the content move. See the snippet below. Is it possible to avoid move the content in this case when the border is displayed?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
</style>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/lib.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#test-id').hover(function() {
console.log("test-id");
$('#test-id').css('border', '5px dotted');
},
function() {
$('#test-id').css('border', 'none');
}
);
});
</script>
</head>
<body style="margin-bottom:0px;margin-top:0px;">
<div>
<div style="width: 300px;">
</div>
<div id="test-id">
jfdjkfjdsfjaldsjfsjf
</div>
</div>
</body>
</html
>
Use CSS 'outline' property instead of border. Which will not occupy element space.
Hope will help you.
Try maintaing the border all the time, and just change the color from transparent to whatever color you want it to have when its visible. You could also use a the background color as the "off" color, but that means it has to overly a solid colored element.
Bin ,
Yes border is also cacluated as part of width. So do one thing give a border before itselef with the same color as background , once you mouseover you can change the color , so that it won't push the other ones down.
This is the behavior how the css render.
You need to set the margin to prevent this.
$('#test-id').css('margin', '-5px');
1 Solution is that you make the with: on hover -2px or another one on normal state, with the border color seted to the box color (or background color of the body).
ex 1:
<body>
<div class="container"></div>
</body>
body { background: #ccc; }
.container { width: 200px; height: 200px; background: #fff; }
.container:hover { width: 198px; border: 1px solid #000; }
ex 2: (best solution)
<body>
<div class="container"></div>
</body>
body { background: #ccc; }
.container { width: 200px; height: 200px; background: #fff; border: 1px solid #fff; } // or #ccc
.container:hover { border: 1px solid #000; }