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

fixes issues #28

Open
wants to merge 1 commit 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
87 changes: 63 additions & 24 deletions lib/Canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var createReactClass = require("create-react-class");
var { View, Platform } = require("react-native");
var { WebView } = require("react-native-webview");

function getRenderHTML(context, render, generator) {
function getRenderHTML(context) {
return `
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0">
<style>
Expand All @@ -16,27 +16,67 @@ function getRenderHTML(context, render, generator) {
<canvas></canvas>
<script>
var canvas = document.querySelector('canvas');
(${render}).call(${context}, canvas);
(${generator}).call('', ${context} , canvas);
</script>"
`;
}
function render(canvas) {
var ctx = canvas.getContext("2d");
var size = this.size;
var fgColor = this.fgColor;
var bgColor = this.bgColor;
canvas.width = size;
canvas.height = size;
canvas.style.left = (window.innerWidth - size) / 2 + "px";
if (window.innerHeight > size) {
canvas.style.top = (window.innerHeight - size) / 2 + "px";
}
ctx.fillRect(0, 0, size, size);
var cells = this.cells;
var cellWidth = this.size / cells.length;
var cellHeight = this.size / cells.length;
var nRoundedWidth = Math.round(cellWidth);
var nRoundedHeight = Math.round(cellHeight);
cells.forEach(function(row, rowIndex) {
row.forEach(function(column, columnIndex) {
var nLeft = columnIndex * cellWidth;
var nTop = rowIndex * cellHeight;
ctx.fillStyle = ctx.strokeStyle = column ? bgColor : fgColor;
ctx.lineWidth = 1;
ctx.fillRect(nLeft, nTop, cellWidth, cellHeight);
ctx.strokeRect(
Math.floor(nLeft) + 0.5,
Math.floor(nTop) + 0.5,
nRoundedWidth,
nRoundedHeight
);
ctx.strokeRect(
Math.ceil(nLeft) - 0.5,
Math.ceil(nTop) - 0.5,
nRoundedWidth,
nRoundedHeight
);
});
});
}

function generateImage(context, canvas) {
if (!window.ReactNativeWebView.postMessage) {
setTimeout(function () {
generateImage.call(this, context, canvas);
}, 100);
} else {
const image = document.createElement("canvas");
const imageContext = image.getContext("2d");
image.width = context.size;
image.height = context.size;
imageContext.fillStyle = context.bgColor;
imageContext.fillRect(0, 0, context.size, context.size);
imageContext.drawImage(canvas, 0, 0);
window.ReactNativeWebView.postMessage(image.toDataURL());
}
function generateImage(context, canvas) {
if (!window.ReactNativeWebView.postMessage) {
setTimeout(function () {
generateImage.call(this, context, canvas);
}, 100);
} else {
const image = document.createElement("canvas");
const imageContext = image.getContext("2d");
image.width = context.size;
image.height = context.size;
imageContext.fillStyle = context.bgColor;
imageContext.fillRect(0, 0, context.size, context.size);
imageContext.drawImage(canvas, 0, 0);
window.ReactNativeWebView.postMessage(image.toDataURL());
}
}

render.call(${context}, canvas);
generateImage.call('', ${context} , canvas);
</script>
`;
}

var Canvas = createReactClass({
Expand All @@ -56,18 +96,17 @@ var Canvas = createReactClass({

render() {
var contextString = JSON.stringify(this.props.context);
var renderString = this.props.render.toString();
var generate = generateImage.toString();
return (
<View style={this.props.style}>
<WebView
automaticallyAdjustContentInsets={false}
scalesPageToFit={false}
contentInset={{ top: 0, right: 0, bottom: 0, left: 0 }}
source={{
html: getRenderHTML(contextString, renderString, generate),
html: getRenderHTML(contextString),
baseUrl: ""
}}

opaque={false}
underlayColor={"transparent"}
overScrollMode="never"
Expand Down
41 changes: 0 additions & 41 deletions lib/QRCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,6 @@ var Canvas = require("./Canvas.js");
var qr = require("qr.js");
var { View, ActivityIndicator } = require("react-native");

function renderCanvas(canvas) {
var ctx = canvas.getContext("2d");
var size = this.size;
var fgColor = this.fgColor;
var bgColor = this.bgColor;
canvas.width = size;
canvas.height = size;
canvas.style.left = (window.innerWidth - size) / 2 + "px";
if (window.innerHeight > size) {
canvas.style.top = (window.innerHeight - size) / 2 + "px";
}
ctx.fillRect(0, 0, size, size);
var cells = this.cells;
var cellWidth = this.size / cells.length;
var cellHeight = this.size / cells.length;
var nRoundedWidth = Math.round(cellWidth);
var nRoundedHeight = Math.round(cellHeight);
cells.forEach(function(row, rowIndex) {
row.forEach(function(column, columnIndex) {
var nLeft = columnIndex * cellWidth;
var nTop = rowIndex * cellHeight;
ctx.fillStyle = ctx.strokeStyle = column ? bgColor : fgColor;
ctx.lineWidth = 1;
ctx.fillRect(nLeft, nTop, cellWidth, cellHeight);
ctx.strokeRect(
Math.floor(nLeft) + 0.5,
Math.floor(nTop) + 0.5,
nRoundedWidth,
nRoundedHeight
);
ctx.strokeRect(
Math.ceil(nLeft) - 0.5,
Math.ceil(nTop) - 0.5,
nRoundedWidth,
nRoundedHeight
);
});
});
}

function getDiff(x, y) {
return Object.keys(x).some(value => x[value] !== y[value]);
}
Expand Down Expand Up @@ -135,7 +95,6 @@ var QRCode = createReactClass({
cells: qr(value).modules
}}
generateImage={this.props.getImageOnLoad}
render={renderCanvas}
onLoad={this.props.onLoad}
onLoadEnd={this.onLoadEnd}
style={{ height: size, width: size }}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-qrcode-generator",
"version": "1.2.2",
"version": "1.2.3",
"description": "react-native qrocode generator",
"main": "index.js",
"types": "index.d.ts",
Expand All @@ -24,6 +24,6 @@
"qr.js": "^0.0.0"
},
"peerDependencies": {
"react-native-webview": "^9.0.1"
"react-native-webview": "^13.0.0"
}
}