Everything disappears when wrapped in a "block" element - html

I am working with this parallaxish 3d effect i found in a codepen. However I need to add an additional wrapper around all the html. When i wrap the html contents in a <div> everything disappears. When I wrap it in a <span> tag everything is fine. Also if i set that <span> tag to display:block; everything disappears again.
Why is this happening when wrapped in a block element?
Thanks!
https://codepen.io/anon/pen/JapeJX

When you add display: block make sure u set its height to 100% as its inner elements have height set in %.
Checkout https://stackoverflow.com/a/5658062/7333443
CodePen: https://codepen.io/anon/pen/QVQJPR
!(function ($doc, $win) {
var screenWidth = $win.screen.width / 2,
screenHeight = $win.screen.height / 2,
$elems = $doc.getElementsByClassName("elem"),
validPropertyPrefix = "",
otherProperty = "perspective(1000px)",
elemStyle = $elems[0].style;
if (typeof elemStyle.webkitTransform == "string") {
validPropertyPrefix = "webkitTransform";
} else if (typeof elemStyle.MozTransform == "string") {
validPropertyPrefix = "MozTransform";
}
$doc.addEventListener("mousemove", function (e) {
var centroX = e.clientX - screenWidth,
centroY = screenHeight - (e.clientY + 13),
degX = centroX * 0.02,
degY = centroY * 0.01,
$elem;
for (var i = 0; i < $elems.length; i++) {
$elem = $elems[i];
$elem.style[validPropertyPrefix] =
otherProperty + "rotateY(" + degX + "deg) rotateX(" + degY + "deg)";
}
});
})(document, window);
/* CREDITS TO DESKTOPOGRAPHY FOR IMAGE USED */
html,
body {
width: 100%;
height: 100%;
}
body {
background: #004382;
overflow: hidden;
}
.wrapper {
transform-style: preserve-3d;
margin: 0 auto;
max-width: 982px;
width: 100%;
height: 100%;
position: relative;
display: flex;
justify-content: center;
align-self: center;
background: url("http://portalpacific.net/img/desk/icon-circles.png")
no-repeat center center;
background-size: contain;
}
.bloc {
height: 100%;
width: 100%;
position: relative;
display: flex;
justify-content: center;
align-self: center;
background-size: contain;
background-position: center;
}
.content {
transform: translateZ(80px) scale(1);
-webkit-transform: translateZ(80px) scale(1);
height: 100%;
width: 100%;
max-width: 720px;
position: absolute;
margin: auto;
color: #fff;
z-index: 3;
}
.content1 {
background: url("http://portalpacific.net/img/desk/Website.png") no-repeat;
background-position: center;
max-width: 982px;
width: 100%;
height: 100%;
position: relative;
display: flex;
justify-content: center;
align-self: center;
background-size: contain;
}
.content p:nth-of-type(1) {
font-size: 36px;
line-height: 60px;
position: absolute;
}
.content p:nth-of-type(2) {
position: absolute;
}
.block {
display: block;
height: 100%;
}
<span class="block">
<div class="wrapper elem" style="transform: perspective(700px) rotateY(0deg) rotateX(0deg);">
<div class="bloc">
<div class="content content1"></div>
<div class="content content2">
</div>
</div>
</div>
</span>

Related

Change fixed background image on specific divs on it

As I'm trying to make a "our story" section, when I scroll, the divs on it move up and the background picture changes when the specific div comes in the center. I want a fixed background image. When the div moves to the middle, the background picture may be changed with ease. Background image and div are related.
I'm still learning how to style, I can't create it correctly.
I've included a reference link so you can see what I'm aiming for.
Reference: https://webflow.com/made-in-webflow/website/Change-Background-Image-on-Scroll
.parent {
background-image: url(https://picsum.photos/1080/1920);
background-size: cover;
background-repeat: no-repeat;
background-position: center;
background-attachment: fixed;
height: 300vh;
text-align: center;
color: white;
}
.div1{
background-color: aqua;
height: 50vh;
margin-top: 90vh;
padding: 20px;
width: 400px;
margin: 90px auto
}
.div2{
background-color: skyblue;
height: 50vh;
padding: 20px;
width: 400px;
margin: 90px auto
}
.div3{
background-color: grey;
height: 50vh;
margin-top: 90vh;
padding: 20px;
width: 400px;
margin: 90px auto
}
<div class="parent">
<div class="div1" >
Content about desert
</div>
<div class="div2" >
Content about mountains
</div>
<div class="div3" >
Content about birds
</div>
</div>
The code below is a rough example of how you might do it. It should give you a starter to experiment with, and things to lookup on the WWW.
var sections;
const backgrounds = [
"url(https://i.picsum.photos/id/604/1080/1920.jpg?hmac=ZGdArZ8jNmfX3SDYlQMsYeGsKy6b_1uBH2WGFt3wYzk)",
"url(https://i.picsum.photos/id/301/1080/1920.jpg?hmac=7cyfS0iaSux6TVm9Wnkg4vtXIWyg_YyhhWYHM6yUTnE)",
"url(https://i.picsum.photos/id/155/1080/1920.jpg?hmac=UZT1atpRomcjj2ijIjwR9C3DY0xtD4On3m5XMNLak6o)"
];
var parent;
var bottomOffset;
function getViewScrollingElements() {
parent = document.getElementById("parent");
sections = document.getElementsByClassName("section");
bottomOffset = window.getComputedStyle(parent).getPropertyValue("height");
bottomOffset = parseInt(bottomOffset.substr(0, bottomOffset.length - 2)) / 3;
// Do first check before a scroll could have occured
checkInView();
}
function checkInView () {
var boundingRect;
for ( var i = 0; i < sections.length; i++ ) {
// Get the extremities of the div
boundingRect = sections[i].getBoundingClientRect();
// Are the div's extremities in view
if ( boundingRect.top < window.innerHeight && boundingRect.bottom >= bottomOffset ) {
parent.style.backgroundImage = backgrounds[i];
break; // Only want one at a time
}
}
}
.background {
background-size: cover;
background-repeat: no-repeat;
background-position: center;
background-attachment: fixed;
}
.parent {
text-align: center;
color: white;
height: 100%;
overflow-y: auto;
}
.section {
height: 70vh;
padding: 20px;
width: 400px;
margin: 90px auto;
opacity: 85%;
}
.section:nth-of-type(1) {
background-color: aqua;
}
.section:nth-of-type(2) {
background-color: skyblue;
}
.section:nth-of-type(3) {
background-color: grey;
}
<body onLoad="getViewScrollingElements()">
<div class="background"></div>
<div id="parent" class="parent" onScroll="checkInView()">
<div class="section">
Content about desert
</div>
<div class="section">
Content about mountains
</div>
<div class="section">
Content about birds
</div>
</div>
</body>
It does not like iframes as is so will not run properly in SOs fiddler, but does work in Chrome and Firefox.

Arrange 2 divs diagonally inside a parent div

I'm trying to arrange 2 divs inside a parent div so that is looks like the parent div is being divided into 2 parts diagonally. The diagram below will show what is required
This is the code i have tried.
App.js
import React, { Component } from "react";
import "./App.css";
class InnerMainDiv extends Component {
constructor() {
super();
this.section = React.createRef();
}
componentDidMount() {
this.handleResize();
window.addEventListener("resize", this.handleResize);
}
componentWillUnmount() {
window.addEventListener("resize", null);
}
handleResize = (WindowSize, event) => {
let h = this.section.current.clientHeight;
let w = this.section.current.clientWidth;
let angle = Math.atan(h / w) * 57.29577;
let rotateProperty = "rotate(" + angle + "deg)";
this.section.current.style.webkitTransform = rotateProperty;
this.section.current.style.transform = rotateProperty;
this.section.current.style.mozTransform = rotateProperty;
};
render() {
return (
<div className="maindiv">
<section ref={this.section}>
<div href="#1" />
</section>
<section ref={this.section}>
<div href="#2" />
</section>
</div>
);
}
}
export default InnerMainDiv;
App.css
html,
body,
div {
height: 100%;
width: 100%;
padding: 0;
margin: 0;
}
div {
overflow: hidden;
position: relative;
}
section {
position: absolute;
top: -100%;
height: 5000vw;
width: 5000vh;
background: #ccc;
-webkit-transform-origin: 0 0;
-moz-transform-origin: 0 0;
transform-origin: 0 0;
}
section + section {
background: #666;
top: 0%;
}
section div {
display: block;
width: 100%;
height: 100%;
cursor: pointer;
}
Any ideas or suggestions on how to achieve this?.
You can use clip-path to achieve this:
.container {
width: 200px;
height: 200px;
position: relative;
}
.container > * {
height: 100%;
background: red;
}
.container :last-child {
position: absolute;
top: 0;
left: 0;
width: 100%;
background: blue;
-webkit-clip-path: polygon(0 0, 100% 0%, 100% 100%);
clip-path: polygon(0 0, 100% 0%, 100% 100%);
}
<div class="container">
<div></div>
<div></div>
</div>
But in case you want more browser support you can use rotation like this:
.container {
width: 200px;
height: 200px;
position: relative;
overflow:hidden;
}
.container > * {
height: 100%;
background: red;
}
.container :last-child {
position: absolute;
top: 0;
left: 0;
width: 141%; /* = 1.41 * 100% --> 1.41 = sqrt(2) */
height: 141%;
background: blue;
transform-origin:top left;
transform:rotate(-45deg);
}
<div class="container">
<div></div>
<div></div>
</div>

Parallax background with inside text

I have parallax background that's moving at a certain speed with javascript. I need to put the text inside those background divs, so each background has its own text block. But when I put the text inside it goes to the top of the page, despite the container it's located. Something in the positions needs to change but I'm not sure what exactly. Any ideas?
HTML
<div id='hero'>
<div class='layer-bg layer' data-depth='0.10' data-type='parallax'>
<img class="logo" />
</div>
<div class='layer-1 layer' data-depth='0.20' data-type='parallax'>
</div>
<div class="relative">
<div class='layer-2 layer' data-depth='0.30' data-type='parallax'>
<div class="video">
Text
</div>
</div>
</div>
<div class='layer-3 layer' data-depth='0.40' data-type='parallax'></div>
</div>
<div id='hero-mobile'></div>
CSS
body {
padding: 0;
margin: 0;
background-color: $bronze;
font-family: 'Playfair Display', serif;
color: $white;
}
//
#hero {
height: $heroHeight;
overflow: hidden;
position: relative;
}
#content {
background-color: $bronze;
}
.layer {
background-position: bottom center;
background-size: auto;
background-repeat: no-repeat;
width: 100%;
height: $heroHeight;
position: fixed;
z-index: -1;
}
.first-section {
padding: 50px 0 20px 0;
}
.text-header {
font-size: 50px;
text-align: center;
}
h1 {
line-height: 120%;
margin-bottom: 30px;
}
p {
color: #ede0d5;
font-size: 18px;
line-height: 150%;
}
// #hero, .layer {
// min-height: 800px;
// }
.layer-bg {
background-image: url('');
height: 4000px!important;
background-position: top center;
width: 100%;
}
.layer-1 {
background-image: url('
');
height: 3000px;
}
.layer-2 {
background-image: url('');
height: 5500px;
}
.layer-3 {
background-image: url('
');
height: 8000px;
}
.layer-4 {
background-image: url('
');
background-position: center bottom;
}
.layer-overlay {
background-image: url('
');
}
.relative {
position: relative;
}
.logo {
margin: 0px auto;
max-width: 600px;
margin-top: 100px;
display: block;
}
JS
(function() {
window.addEventListener('scroll', function(event) {
var depth, i, layer, layers, len, movement, topDistance, translate3d;
topDistance = this.pageYOffset;
layers = document.querySelectorAll("[data-type='parallax']");
for (i = 0, len = layers.length; i < len; i++) {if (window.CP.shouldStopExecution(1)){break;}
layer = layers[i];
depth = layer.getAttribute('data-depth');
movement = -(topDistance * depth);
translate3d = 'translate3d(0, ' + movement + 'px, 0)';
layer.style['-webkit-transform'] = translate3d;
layer.style['-moz-transform'] = translate3d;
layer.style['-ms-transform'] = translate3d;
layer.style['-o-transform'] = translate3d;
layer.style.transform = translate3d;
}
window.CP.exitedLoop(1);
});
}).call(this);
I don't why you are using 8000px height in your CSS but
check the link may be it will be helpful for you
https://www.w3schools.com/howto/tryhow_css_parallax_demo.htm
Its because you fixed the position of every .layer class div that's why they all are in same position of your DOM.
And overflow: hidden; of your main div #hero
Solution
You have to remove fixed position of your .layer class and overflow: hidden;.
Example:- see fiddle

Need to slide one DIV on another DIV. Using only CSS

There should be a simple solution, but I can't work one out. I need div with class of geltona to slide onto div with class of zydra. I must use only css, but I can't. It might be obvious, but I can't find a solution as I am only using floats and % in width and height, so I can't really set a location where it should go by keyframes.
html,
body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
p {
margin: 10px;
}
.header {
height: 5%;
width: 100%;
}
.geltona {
height: 15%;
width: 50%;
background-color: yellow;
float: left;
}
.zydra {
height: 15%;
width: 50%;
background-color: lightblue;
float: right;
}
.balta {
height: 30%;
width: 70%;
background-color: white;
float: left;
}
.juoda {
height: 75%;
width: 30%;
background-color: black;
float: right;
}
.oranzine {
height: 20%;
width: 35%;
background-color: orange;
display: inline;
float: left;
}
.melyna {
position: relative;
height: 45%;
width: 35%;
background-color: blue;
float: right;
}
.zalia {
height: 25%;
width: 35%;
background-color: green;
float: left;
}
.ruda {
height: 5%;
width: 100%;
background-color: brown;
float: left;
}
/* ANIMACIJOS */
/* 3. Blokas animuotai nukeliauja ant gretimo bloko, pilnai uždengęs gretimą bloką – išnyksta */
/* 20. Pasisuka nuo 45 laipsnių iki 0 laipsnių ir padidėja 30%; */
.zalia:hover {
animation: sukasi 3s;
}
#keyframes sukasi {
0%{transform: rotate(45deg)}
100%{transform: rotate(0deg) scale(1.3)};
}
/* 21. Nuotrauka atslenka iš viršaus ir mažėja (trukmė 5 sec); */
.header img {
position: absolute;
top: -145px;
max-width: 145px;
max-height: 145px;
background: transparent;
transition: 5s;
}
.header:hover img {
transition: 5s;
top: 0;
max-width: 45px;
max-height: 45px;
}
/* MEDIA QUERIES */
#media only screen and (max-width: 768px) and (max-height: 1024px){
.geltona {
height: 25%;
width: 100%;
}
.zydra {
display: none;
}
.balta {
height: 20%;
width: 70%;
}
.juoda {
height: 65%;
width: 30%;
}
.oranzine {
display: none;
}
.melyna {
height: 45%;
width: 40%;
}
.zalia {
height: 45%;
width: 30%;
}
}
<!DOCTYPE html>
<html>
<head>
<title>IPP Kursinis</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width" content="initial-scale=1">
<link rel="stylesheet" type="text/css" href="src/css/main.css">
</head>
<body>
<div class="header">
<img src="img/foto.jpg">
</div>
<div class="geltona"></div>
<div class="zydra"
id="keiciamaSpalva"
onmouseover="changeBackground(this.id, 'red', 'yellow');"
onmouseout="changeBackground(this.id, 'lightblue', 'black');">
<p>Lorem ipsum</p>
</div>
<div class="balta"
id="balta"
onmouseover="showClass();"
onmouseout="removeClass();">
</div>
<div class="juoda"></div>
<div class="oranzine"></div>
<div class="melyna">
<p id="demo"></p>
</div>
<div class="zalia"></div>
<div class="ruda"></div>
<!-- JAVASCRIPT prasideda nuo čia -->
<script type="text/javascript">
/* Keičiama teksto ir fono spalva pagal nurodymus. */
function changeBackground(id, color, textColor) {
document.getElementById(id).style.background = color;
document.getElementById(id).style.color = textColor;
};
/* Parodome bloko klasę ir ją ištriname, kad nesipildytų visas blokas*/
function showClass() {
var element = document.getElementById('balta');
element.innerHTML = element.innerHTML + "<p> Šio bloko klasė: "+element.className+"</p>";
};
function removeClass() {
document.getElementById('balta').innerHTML = "";
};
/* Rodoma lango, ekrano ir slankiklių informacija, tik tuomet, kai keičiame ekrano dydį. */
window.onresize = displayWindowSize;
function displayWindowSize(){
document.getElementById("demo").innerHTML =
"Ekrano plotis: " + screen.width + "<br>" +
"Ekrano aukštis: " + screen.height + "<br>" +
"Lango plotis: " + window.innerWidth + "<br>" +
"Lango aukštis: " + window.innerHeight + "<br>" +
"Slankiklio horizontali padėtis: " + window.scrollX + "<br>" +
"Slankiklio vertikali padėtis: " + window.scrollY;
};
</script>
</body>
</html>
With the least amount of changes for this code, I came up with this.
Wrap your two neighbouring containers (geltona and zydra) in another container (gelzy here) and give it the position:relative and the two child containers the position:absolute with left:0 and right:0 instead of the floats.
Make use of the transition on hover to move the geltona (with higher z-index)to 50% of the left to overlap on the blue container
.gelzy{
position:relative;
height:15%;
width:100%;
}
.geltona {
height: 100%;
width: 50%;
background-color: yellow;
left:0;
position: absolute;
z-index:2;
transition:left 1s ease-in-out;
}
.zydra {
height: 100%;
width: 50%;
background-color: lightblue;
right:0;
position: absolute;
}
.geltona:hover {
left:50%;
}
html,
body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
p {
margin: 10px;
}
.header {
height: 5%;
width: 100%;
}
.gelzy{
position:relative;
height:15%;
width:100%;
}
.geltona {
height: 100%;
width: 50%;
background-color: yellow;
left:0;
position: absolute;
z-index:2;
transition:left 1s ease-in-out;
}
.zydra {
height: 100%;
width: 50%;
background-color: lightblue;
right:0;
position: absolute;
}
.geltona:hover {
left:50%;
}
.balta {
height: 30%;
width: 70%;
background-color: white;
float: left;
}
.juoda {
height: 75%;
width: 30%;
background-color: black;
float: right;
}
.oranzine {
height: 20%;
width: 35%;
background-color: orange;
display: inline;
float: left;
}
.melyna {
position: relative;
height: 45%;
width: 35%;
background-color: blue;
float: right;
}
.zalia {
height: 25%;
width: 35%;
background-color: green;
float: left;
}
.ruda {
height: 5%;
width: 100%;
background-color: brown;
float: left;
}
/* ANIMACIJOS */
/* 3. Blokas animuotai nukeliauja ant gretimo bloko, pilnai uždengęs gretimą bloką – išnyksta */
/* 20. Pasisuka nuo 45 laipsnių iki 0 laipsnių ir padidėja 30%; */
.zalia:hover {
animation: sukasi 3s;
}
#keyframes sukasi {
0% {
transform: rotate(45deg)
}
100% {
transform: rotate(0deg) scale(1.3)
}
;
}
/* 21. Nuotrauka atslenka iš viršaus ir mažėja (trukmė 5 sec); */
.header img {
position: absolute;
top: -145px;
max-width: 145px;
max-height: 145px;
background: transparent;
transition: 5s;
}
.header:hover img {
transition: 5s;
top: 0;
max-width: 45px;
max-height: 45px;
}
/* MEDIA QUERIES */
#media only screen and (max-width: 768px) and (max-height: 1024px) {
.geltona {
height: 25%;
width: 100%;
}
.zydra {
display: none;
}
.balta {
height: 20%;
width: 70%;
}
.juoda {
height: 65%;
width: 30%;
}
.oranzine {
display: none;
}
.melyna {
height: 45%;
width: 40%;
}
.zalia {
height: 45%;
width: 30%;
}
}
<!DOCTYPE html>
<html>
<head>
<title>IPP Kursinis</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width" content="initial-scale=1">
<link rel="stylesheet" type="text/css" href="src/css/main.css">
</head>
<body>
<div class="header">
<img src="img/foto.jpg">
</div>
<div class="gelzy">
<div class="geltona"></div>
<div class="zydra" id="keiciamaSpalva" onmouseover="changeBackground(this.id, 'red', 'yellow');" onmouseout="changeBackground(this.id, 'lightblue', 'black');">
<p>Lorem ipsum</p>
</div>
</div>
<div class="balta" id="balta" onmouseover="showClass();" onmouseout="removeClass();">
</div>
<div class="juoda"></div>
<div class="oranzine"></div>
<div class="melyna">
<p id="demo"></p>
</div>
<div class="zalia"></div>
<div class="ruda"></div>
<!-- JAVASCRIPT prasideda nuo čia -->
<script type="text/javascript">
/* Keičiama teksto ir fono spalva pagal nurodymus. */
function changeBackground(id, color, textColor) {
document.getElementById(id).style.background = color;
document.getElementById(id).style.color = textColor;
};
/* Parodome bloko klasę ir ją ištriname, kad nesipildytų visas blokas*/
function showClass() {
var element = document.getElementById('balta');
element.innerHTML = element.innerHTML + "<p> Šio bloko klasė: " + element.className + "</p>";
};
function removeClass() {
document.getElementById('balta').innerHTML = "";
};
/* Rodoma lango, ekrano ir slankiklių informacija, tik tuomet, kai keičiame ekrano dydį. */
window.onresize = displayWindowSize;
function displayWindowSize() {
document.getElementById("demo").innerHTML =
"Ekrano plotis: " + screen.width + "<br>" +
"Ekrano aukštis: " + screen.height + "<br>" +
"Lango plotis: " + window.innerWidth + "<br>" +
"Lango aukštis: " + window.innerHeight + "<br>" +
"Slankiklio horizontali padėtis: " + window.scrollX + "<br>" +
"Slankiklio vertikali padėtis: " + window.scrollY;
};
</script>
</body>
</html>

How to print a large canvas across multiple page widths in browser?

My application needs to print out an arbitrarily large canvas that can span multiple page width and height widths.
There was a similar question some time back where it was claimed the browser won't print to multiple page widths.
Since this was a while back I am wondering if it is still true. Also, what strategies are available to print out a large canvas without splitting it up?
var canvas = document.getElementById("canvas1");
function draw_a() {
var context = canvas.getContext("2d");
// // LEVER
//plane
context.fillStyle = '#aaa';
context.fillRect(25, 90, 2500, 400);
}
$(document).ready(function() {
draw_a();
});
canvas {
border: 1px dotted;
}
.printOnly {
display: none;
}
#media print {
html,
body {
height: 100%;
background-color: yellow;
}
.myDivToPrint {
background-color: yellow;
/*
height: 100%;
width: 100%;
position: fixed;*/
top: 0;
left: 0;
margin: 0;
}
.no-print,
.no-print * {
display: none !important;
}
.printOnly {
display: block;
}
}
#media print and (-ms-high-contrast: active),
(-ms-high-contrast: none) {
html,
body {
height: 100%;
background-color: yellow;
}
.myDivToPrint {
background-color: yellow;
/*
height: 100%;
width: 100%;
position: fixed;*/
top: 0;
left: 0;
margin: 0;
padding: 15px;
font-size: 14px;
line-height: 18px;
position: absolute;
display: flex;
align-items: center;
justify-content: center;
-webkit-transform: rotate(90deg);
-moz-transform: rotate(90deg);
-o-transform: rotate(90deg);
-ms-transform: rotate(90deg);
transform: rotate(90deg);
}
.no-print,
.no-print * {
display: none !important;
}
.printOnly {
display: block;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="window.print();" class="no-print">Print Canvas</button>
<div class="myDivToPrint">
<div class="Aligner-item">
<canvas height="2500px" width="4000px" id="canvas1"></canvas>
<div class="printOnly Aligner-item--bottom"> Print Only</div>
</div>
</div>
It does seem that browsers will split up a large canvas into multiple pages. I tested on MacOS Sierra using latest chrome and safari browsers.
A possible approach for printing a canvas is to first transform it to a data URI containing a representation of the image using canvas.toDataURL(). You can then manipulate the image dimensions prior to printing.
"<img src='" + canvas.toDataURL() + "' height='500px' width='500px' />'"
In the following example, the large 4500px by 4500px canvas is translated into an img and placed inside an iframe, used for printing. You can probably append the image to the original document and than print that specific element, but the iframe may be more flexible to handle print output. You can manipulate the img dimensions according to your requirements and print a scaled representation of the canvas. Note that I hardcoded the width and height of the image but this can be calculated and changed as needed for printing.
Due to iframe cross-origin restrictions, the code snippet below will not work here, but it does work on this jsfiddle.
The scaled 500px by 500px image representing the canvas fits on one page when printed.
var canvas = document.getElementById("canvas1");
function draw_a() {
var context = canvas.getContext("2d");
// // LEVER
//plane
context.fillStyle = '#aaa';
context.fillRect(25, 90, 4500, 4500);
}
print = function() {
window.frames["myFrame"].focus();
window.frames["myFrame"].print();
}
function setupPrintFrame() {
$('<iframe id="myFrame" name="myFrame">').appendTo("body").ready(function(){
setTimeout(function(){
$('#myFrame').contents().find('body').append("<img src='" + canvas.toDataURL() + "' height='500px' width='500px' />'");
},50);
});
}
$(document).ready(function() {
draw_a();
setupPrintFrame();
});
canvas {
border: 1px dotted;
}
.printOnly, #myFrame {
display: none;
}
#media print {
html,
body {
height: 100%;
background-color: yellow;
}
.myDivToPrint {
background-color: yellow;
/*
height: 100%;
width: 100%;
position: fixed;*/
top: 0;
left: 0;
margin: 0;
}
.no-print,
.no-print * {
display: none !important;
}
.printOnly {
display: block;
}
}
#media print and (-ms-high-contrast: active),
(-ms-high-contrast: none) {
html,
body {
height: 100%;
background-color: yellow;
}
.myDivToPrint {
background-color: yellow;
/*
height: 100%;
width: 100%;
position: fixed;*/
top: 0;
left: 0;
margin: 0;
padding: 15px;
font-size: 14px;
line-height: 18px;
position: absolute;
display: flex;
align-items: center;
justify-content: center;
-webkit-transform: rotate(90deg);
-moz-transform: rotate(90deg);
-o-transform: rotate(90deg);
-ms-transform: rotate(90deg);
transform: rotate(90deg);
}
.no-print,
.no-print * {
display: none !important;
}
.printOnly {
display: block;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="print()" class="no-print">Print Canvas</button>
<div class="myDivToPrint">
<div class="Aligner-item">
<canvas height="4500px" width="4500px" id="canvas1"></canvas>
<div class="printOnly Aligner-item--bottom"> Print Only</div>
</div>
</div>
#media print {
#page {
size: 297mm 210mm; /* landscape */
/* you can also specify margins here: */
margin: 25mm;
margin-right: 45mm; /* for compatibility with both A4 and Letter */
}
}
var canvas = document.getElementById("canvas1");
function draw_a() {
var context = canvas.getContext("2d");
// // LEVER
//plane
context.fillStyle = '#aaa';
context.fillRect(25, 90, 2500, 400);
}
$(document).ready(function() {
draw_a();
});
canvas {
border: 1px dotted;
}
.printOnly {
display: none;
}
#media print {
#page {
size: 297mm 210mm; /* landscape */
/* you can also specify margins here: */
margin: 25mm;
margin-right: 45mm; /* for compatibility with both A4 and Letter */
}
html,
body {
height: 100%;
background-color: yellow;
}
.myDivToPrint {
background-color: yellow;
/*
height: 100%;
width: 100%;
position: fixed;*/
top: 0;
left: 0;
margin: 0;
}
.no-print,
.no-print * {
display: none !important;
}
.printOnly {
display: block;
}
}
#media print and (-ms-high-contrast: active),
(-ms-high-contrast: none) {
html,
body {
height: 100%;
background-color: yellow;
}
.myDivToPrint {
background-color: yellow;
/*
height: 100%;
width: 100%;
position: fixed;*/
top: 0;
left: 0;
margin: 0;
padding: 15px;
font-size: 14px;
line-height: 18px;
position: absolute;
display: flex;
align-items: center;
justify-content: center;
-webkit-transform: rotate(90deg);
-moz-transform: rotate(90deg);
-o-transform: rotate(90deg);
-ms-transform: rotate(90deg);
transform: rotate(90deg);
}
.no-print,
.no-print * {
display: none !important;
}
.printOnly {
display: block;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="window.print();" class="no-print">Print Canvas</button>
<div class="myDivToPrint">
<div class="Aligner-item">
<canvas height="2500px" width="4000px" id="canvas1"></canvas>
<div class="printOnly Aligner-item--bottom"> Print Only</div>
</div>
</div>
Try this!
var canvas = document.getElementById("canvas1");
function draw_a() {
var context = canvas.getContext("2d");
context.fillStyle = '#aaa';
context.fillRect (25, 90, 2500, 400);
}
$(document).ready(function(){
draw_a();
});
#page Section1 {
size:8.27in 11.69in;
margin:0;
mso-header-margin:0;
mso-footer-margin:0;
mso-paper-source:0;
}
<button onclick="window.print();" class="no-print">Print Canvas</button>
<div class="myDivToPrint">
<div class="Aligner-item">
<canvas height="2500px" width="4000px" id="canvas1" style="border: solid 10px #000;"></canvas>
</div>
</div>
I just tested this fiddle in both browsers firefox and chrome using a localhost environment and it worked within both. Here is the original js fiddle
And here is the html I tested
var canvas = document.getElementById("canvas1");
function draw_a() {
var context = canvas.getContext("2d");
// // LEVER
//plane
context.fillStyle = '#aaa';
context.fillRect(25, 90, 2500, 400);
}
$(document).ready(function() {
draw_a();
});
div.sizePage {
color: #333;
}
canvas {
border: 1px dotted;
}
.printOnly {
display: none;
}
#media print {
html,
body {
height: 100%;
background-color: yellow;
}
.myDivToPrint {
background-color: yellow;
/*
height: 100%;
width: 100%;
position: fixed;*/
top: 0;
left: 0;
margin: 0;
}
.no-print,
.no-print * {
display: none !important;
}
.printOnly {
display: block;
}
}
#media print and (-ms-high-contrast: active),
(-ms-high-contrast: none) {
html,
body {
height: 100%;
background-color: yellow;
}
.myDivToPrint {
background-color: yellow;
/*
height: 100%;
width: 100%;
position: fixed;*/
top: 0;
left: 0;
margin: 0;
padding: 15px;
font-size: 14px;
line-height: 18px;
position: absolute;
display: flex;
align-items: center;
justify-content: center;
-webkit-transform: rotate(90deg);
-moz-transform: rotate(90deg);
-o-transform: rotate(90deg);
-ms-transform: rotate(90deg);
transform: rotate(90deg);
}
.no-print,
.no-print * {
display: none !important;
}
.printOnly {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="window.print();" class="no-print">Print Canvas</button>
<div class="myDivToPrint">
<div class="Aligner-item">
<canvas height="2500px" width="4000px" id="canvas1"></canvas>
<div class="printOnly Aligner-item--bottom"> Print Only</div>
</div>
</div>
So I thinks it's safe to say that it is supported in both browsers now.
I am using the most recent update on both browsers.
It is impossible to handle this problem using plain CSS styles.
I recommend to clone element to print multiple times (in this case), put copies after each other and make them "print only" using CSS. Additionally, canvas can't be just cloned - it needs to be redrawn for each copy.
Number of copies depends on the element and page widths.
Default page width is 210mm, it can be converted to px (Pixel to Centimeter?) and compared to the element's width.
When we have page width in pixels, we can set negative left margin for each copy respectively. Having that, entire canvas will be "divided" into columns and printed from top to bottom.
In order to have each copy printed starting from the new page, simple use this CSS rule:
page-break-before: always;
There is a lot of hardcoded things, however I think that you could use it to build a generic solution for your problem.
var divide = function(selector, pageWidth) {
var elementToDivide = document.querySelector(selector);
var widthPx = elementToDivide.offsetWidth;
var pageWidthPx = pageWidth * 3.7795;
for (var i = 1; i <= parseInt(widthPx/pageWidthPx); i++) {
var clone = elementToDivide.cloneNode(true);
elementToDivide.parentNode.appendChild(clone);
draw_a(document.getElementsByTagName("canvas"))
clone.style.marginLeft = "-" + (pageWidthPx * i) + "px";
clone.className += " printOnly";
}
}
var standardPrint = window.print;
window.print = function() {
if (!window.pagesDivided) {
divide(".myDivToPrint", 210);
window.pagesDivided = true;
}
standardPrint();
};
function draw(canvas) {
var context = canvas.getContext("2d");
var grd = context.createLinearGradient(0, 0, 4000, 2500);
grd.addColorStop(0, "yellow");
grd.addColorStop(1, "red");
context.fillStyle = grd;
context.fillRect(25, 25, 4000, 2500);
}
function draw_a(elem) {
if (elem.length != null && elem.length > 1) {
for (var i = 0; i < elem.length; i++) {
draw(elem[i]);
}
} else {
draw(elem);
}
}
$(document).ready(function() {
draw_a(document.getElementById("canvas1"));
});
canvas {
border: 5px dashed;
}
.printOnly {
display: none;
}
.myDivToPrint {
float: left;
}
#media print {
#page {
size: 297mm 210mm;
margin: 0mm;
margin-right: 0mm;
}
html,
body {
height: 100%;
background-color: yellow;
}
.myDivToPrint {
page-break-before: always;
background-color: yellow;
margin: 0;
}
.no-print,
.no-print * {
display: none !important;
}
.printOnly {
display: block;
page-break-before: always;
}
}
#media print and (-ms-high-contrast: active),
(-ms-high-contrast: none) {
html,
body {
height: 100%;
background-color: yellow;
}
.myDivToPrint {
background-color: yellow;
top: 0;
left: 0;
margin: 0;
padding: 15px;
font-size: 14px;
line-height: 18px;
align-items: center;
justify-content: center;
}
.no-print,
.no-print * {
display: none !important;
}
.printOnly {
display: block;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="window.print();" class="no-print">Print Canvas</button>
<div class="myDivToPrint">
<div class="Aligner-item">
<canvas height="2500px" width="4000px" id="canvas1"></canvas>
<div class="printOnly Aligner-item--bottom"> Print Only</div>
</div>
</div>