Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke strict mode in the anonymous function wrapper #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
__Domvas implements the missing piece that connects the DOM and Canvas__. It gives to the ability to take arbitrary DOM content and paint it to a Canvas of your choice.

## Usage

```js
var canvas = document.getElementById("test");
var context = canvas.getContext('2d');
Expand All @@ -15,10 +16,12 @@ domvas.toImage(document.getElementById("dom"), function() {
```

## Syntax

```js
domvas.toImage(domElement, readyCallback, width, height, left, top);
```
readyCallback's 'this' and first argument points to a valid, preloaded image node that you can simply draw to your canvas context.

readyCallback's `this` and first argument points to a valid, preloaded image node that you can simply draw to your canvas context.

## How it works

Expand Down
28 changes: 15 additions & 13 deletions src/domvas.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
"use strict";

(function() {
(function(window, document) {
"use strict";

var supportsCSSText = getComputedStyle(document.body).cssText !== "";

function copyCSS(elem, origElem, log) {
function copyCSS(elem, origElem) {

var computedStyle = getComputedStyle(origElem);

if(supportsCSSText) {
if (supportsCSSText) {
elem.style.cssText = computedStyle.cssText;

} else {
Expand All @@ -30,15 +29,17 @@
var origChildren = origElem.querySelectorAll('*');

// copy the current style to the clone
copyCSS(elem, origElem, 1);
copyCSS(elem, origElem);

// collect all nodes within the element, copy the current style to the clone
Array.prototype.forEach.call(children, function(child, i) {
copyCSS(child, origChildren[i]);
});

var elstyle = elem.style;

// strip margins from the outer element
elem.style.margin = elem.style.marginLeft = elem.style.marginTop = elem.style.marginBottom = elem.style.marginRight = '';
elstyle.margin = elstyle.marginLeft = elstyle.marginTop = elstyle.marginBottom = elstyle.marginRight = '';

}

Expand All @@ -62,11 +63,12 @@

// Create well formed data URL with our DOM string wrapped in SVG
var dataUri = "data:image/svg+xml," +
"<svg xmlns='http://www.w3.org/2000/svg' width='" + ((width || origElem.offsetWidth) + left) + "' height='" + ((height || origElem.offsetHeight) + top) + "'>" +
"<foreignObject width='100%' height='100%' x='" + left + "' y='" + top + "'>" +
serialized +
"</foreignObject>" +
"</svg>";
"<svg xmlns='http://www.w3.org/2000/svg' width='" +
((width || origElem.offsetWidth) + left) + "' height='" +
((height || origElem.offsetHeight) + top) + "'>" +
"<foreignObject width='100%' height='100%' x='" + left + "' y='" + top + "'>" +
serialized +
"</foreignObject></svg>";

// create new, actual image
var img = new Image();
Expand All @@ -83,5 +85,5 @@

};

})();
})(window, document);

2 changes: 1 addition & 1 deletion src/domvas.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.