I have an application which prints barcodes using images for each column (p.png) and the same for spacing (b.png) as follow:
<img src="imagens/p.png" width="1" height="50" border="0">
<img src="imagens/b.png" width="1" height="50" border="0">
<img src="imagens/p.png" width="1" height="50" border="0">
<img src="imagens/b.png" width="2" height="50" border="0">
<img src="imagens/p.png" width="2" height="50" border="0">
[...]
I didn't use any of css to change img in #media print.
The problem is: Chrome doesn't print some imgs ( p.png ).
1. Original barcode
2. Print in Chrome
3. Print in Firefox
Alternative solution: Use javascript canvas to solve this problem.
I create a code in codePen for use in all browsers:
https://codepen.io/eltonsrc/pen/OpXyrQ
HTML:
<div style="width: 600px">
<canvas id="barcode" class="barcode">
Code for browsers without canvas support. For example:
<img src="blackBar.gif" width="1" height="50">
<img src="whiteBar.gif" width="1" height="50">
</canvas>
</div>
CSS:
.barcode {
width: 100%;
height: 50px;
}
Javascript:
var barCode = (function (){
this.WHITE = 'rgb(255, 255, 255)';
this.BLACK = 'rgb(0, 0, 0)';
this.THIN_BAR = 1;
this.THICK_BAR = 3;
this.lastPixel = 0;
this.drawBar = function (barWidth, color) {
this.ctx.fillStyle = color;
this.ctx.fillRect(this.lastPixel, 0, barWidth, this.canvas.height);
this.lastPixel += barWidth;
};
this.draw2of5BarCode = function (barcodeNumber) {
var barCodes = ['00110', '10001', '01001', '11000', '00101', '10100', '01100', '00011', '10010', '01010'];
for (var f1 = 9; f1 >= 0; f1--) {
for (var f2 = 9; f2 >= 0; f2--) {
var f = f1 * 10 + f2;
var texto = '';
for (var i = 0; i < 5; i++) {
texto += barCodes[f1].substr(i, 1) + barCodes[f2].substr(i, 1);
}
barCodes[f] = texto;
}
}
// begin of barcode
this.drawBar(this.THIN_BAR, this.BLACK);
this.drawBar(this.THIN_BAR, this.WHITE);
this.drawBar(this.THIN_BAR, this.BLACK);
this.drawBar(this.THIN_BAR, this.WHITE);
if (barcodeNumber.length % 2 != 0) {
barcodeNumber = '0' + barcodeNumber;
}
do {
var i = Number(barcodeNumber.substr(0, 2));
if (barcodeNumber.length == 2) {
barcodeNumber = '';
} else {
barcodeNumber = barcodeNumber.substr(2, barcodeNumber.length - 2);
}
var f = barCodes[i];
for (i = 0; i < 10; i += 2) {
if (f.substr(i, 1) == '0') {
this.drawBar(this.THIN_BAR, this.BLACK);
} else {
this.drawBar(this.THICK_BAR, this.BLACK);
}
if (f.substr(i + 1, 1) == '0') {
this.drawBar(this.THIN_BAR, this.WHITE);
} else {
this.drawBar(this.THICK_BAR, this.WHITE);
}
}
} while(barcodeNumber.length > 0);
this.drawBar(this.THICK_BAR, this.BLACK);
this.drawBar(this.THIN_BAR, this.WHITE);
this.drawBar(this.THIN_BAR, this.BLACK);
}
this.drawBarcode = function (canvasId, barcodeNumber) {
this.canvas = document.getElementById(canvasId);
// verify canvas support
if (!this.canvas.getContext) {
return;
}
this.lastPixel = 0;
this.ctx = this.canvas.getContext('2d');
this.draw2of5BarCode(barcodeNumber);
};
return this;
})();
barCode.drawBarcode('barcode', '856000000005227702201707619934651263800000000003');
Alternative Solution: Use SVG to generate your barcodes!
I found a library to which creates 'ITF' barcodes without errors: https://github.com/kreativekorp/barcode
include 'barcode.php';
$generator = new barcode_generator();
/* Generate SVG markup. */
$symbology = 'itf';
$data = '23790198019000000053081017674607670300000001000';
$options['w'] = '50';
$options['p'] = '2';
$svg = $generator->render_svg($symbology, $data, $options);
echo $svg;
Many thanks to #kreativekorp for this awesome php library.
Related
This code is for writing text on the image .. I had trouble saving the image, I cannot save it to the phone
I got a solution here and succeeded in saving the pictures to the phone ..
In answer number 1
Save image to local automatically
but I couldn't use it for my code
function myFunction() {
document.getElementById("canvas").style.display = "block";
var x = document.getElementById("myText").value;
//demoo document.getElementById("demo").innerHTML = x;
addTextToImage("https://l.top4top.io/p_1549mpaz31.png", x);
}
function addTextToImage(imagePath, text) {
var circle_canvas = document.getElementById("canvas");
var context = circle_canvas.getContext("2d");
// Draw Image function
var img = new Image();
img.src = imagePath;
img.onload = function () {
context.font = "40px Cairo";
context.textAlign = "center";
context.drawImage(img, 0, 0);
context.lineWidth = 1;
context.fillStyle = "#ffffff";
context.lineStyle = "#ffff00";
context.fillText(text, 520, 790);
}
}
#import url('https://fonts.googleapis.com/css?family=Cairo:300,400,600,700,900');
.container {
position: relative;
font-family: Arial;
font-family: 'Cairo';
}
<canvas style="display:none" id="canvas" width="1080" height="1080"></canvas>
<input type="text" id="myText" value="write your name">
<button onclick="myFunction()">button</button> ..
<p id="demoo"></p>
I have added a codepen for you. Please check and let me know if you have any more query. I just focused on how you can download image from canvas. I think that is your most concerned issue.
HTML
<input type="text" id="text" placeholder="write your name">
<button onclick="writeText()">Show Image</button>
<button onclick="download()">Download Image</button>
<canvas id="canvas" width="200" height="200"></canvas>
JS
function writeText() {
console.log("Hello")
var canvas = document.getElementById("canvas");
var name = document.getElementById("text").value;
if(name.trim() == "" || name.trim().length == 0) {
alert("Write something in the text box first");
return;
}
var ctx = canvas.getContext("2d");
ctx.font = "30px Arial";
ctx.fillText(name, 10, 50);
}
function download() {
writeText()
var link = document.createElement('a');
link.download = 'text.png';
link.href = document.getElementById("canvas").toDataURL()
link.click();
}
https://codepen.io/kousik-mandal/pen/RwWjmqM
Thank you.
I need to filter table rows by category so class filterTr is added to element <tr> but then element <tr> resizes in width and no longer aligns with element <th> above.
I don't know why this happens. The snippet below is a simplified version but the same thing happens here:
filterSelection("all")
function filterSelection(c) {
var x, i;
x = document.getElementsByClassName("filterTr");
if (c == "all") c = "";
for (i = 0; i < x.length; i++) {
w3RemoveClass(x[i], "show");
if (x[i].className.indexOf(c) > -1) w3AddClass(x[i], "show");
}
}
function w3AddClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
if (arr1.indexOf(arr2[i]) == -1) {element.className += " " + arr2[i];}
}
}
function w3RemoveClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
while (arr1.indexOf(arr2[i]) > -1) {
arr1.splice(arr1.indexOf(arr2[i]), 1);
}
}
element.className = arr1.join(" ");
}
var btnContainer = document.getElementById("sowClndr-filter");
var btns = btnContainer.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function(){
var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
});
}
/* Table filter */
.filterTr {
display: none;
}
.filterBtn {
margin-top:-10px!important;
background-color:rgba(55,175,75,1.00)!important;
color:rgba(255,255,255,1.00)!important;
border:1px solid rgba(55,175,75,1.00)!important;
text-align:left!important;
font-weight:400!important;
text-overflow:ellipsis!important;
white-space:nowrap!important;
overflow:hidden!important;
}
.filterBtn:hover {
background-color:rgba(255,255,255,0.00)!important;
color:rgba(0,145,255,1.00)!important;
border:1px solid rgba(0,145,255,1.00)!important;
}
.btn.active {
background-color:rgba(255,255,255,0.00)!important;
color:rgba(0,145,255,1.00)!important;
border:1px solid rgba(0,145,255,1.00)!important;
}
.show {
display: block;
}
/* Table styles */
.sowClndr-tr {
background-color:rgba(255,255,255,1.00)!important;
border:0!important;
}
.sowClndr-tr:hover {
background-color:rgba(55,175,75,0.15)!important;
border:0!important;
}
<div id="sowClndr-filter">
<button class="btn btn-primary filterBtn active" onclick="filterSelection('all')"> All selections</button>
<button class="btn btn-primary filterBtn" onclick="filterSelection('A')"> selection A</button>
<button class="btn btn-primary filterBtn" onclick="filterSelection('B')"> selection B</button>
</div>
<table id="myTable">
<tr class="header">
<th style="width:60%;">Name</th>
<th style="width:40%;">Country</th>
</tr>
<tr class="sowClndr-tr filterTr A">
<td>S1 fname lname</td>
<td>lorem ipsum</td>
</tr>
<tr class="sowClndr-tr filterTr B">
<td>lorem ipsum</td>
<td>lorem ipsum</td>
</tr>
</table>
SOLVED
The CSS style display:block; for class .show messes up the table layout because class .show is added to <tr> using JS to make it visible. The solution is to use CSS style display:table-row; instead, now it works. I did not know that this style existed untill now.
i am a trainee software engineer and i am working on angular CLI for my learning process. it is image cropping stuff on canvas. i draw a circle on when click on it.
My question is how to move the circle with mousedown and stop it when mouse up
and take (x ,y) coordinate as gloable to change final crop image.
here is my html...
<div class="row">
<div class="col-sm-6">
<canvas #layout1 id="layout1" width="500" height="300"
(mousedown)="mouseDown($event)"
(mouseup)="mouseUp($event)"
(mousemove)="coordinates($event)">
This text is displayed if your browser does not support HTML5 Canvas.
</canvas>
</div>
<div class="col-sm-6">
<img class="crope-image" src="{{profPic}}" #photo style="width:500px; height:300px;">
</div>
</div>
here is my typscriptfile...
export class FourComponent implements OnInit {
....some code here...
#ViewChild('layout1') canvas;
#ViewChild('photo') photo;
mouseDown(event: MouseEvent): void {
this.rect.startX = event.pageX - this.offsetLeft;
this.rect.startY = event.pageY - this.offsetTop;
this.drag = true;
const _canvas = this.canvas.nativeElement;
this.event = event;
console.log('kkkk');
this.context.strokeStyle = 'black';
this.context.beginPath();
this.context.arc(this.clientX, this.clientY - 130, 50, 0, 2 * Math.PI);
this.context.stroke();
const _photo = this.photo.nativeElement;
_photo.setAttribute('src', _canvas.toDataURL('image/png'));
this.profPic = _canvas.toDataURL('image/png')
}
mouseUp(event: MouseEvent): void {
this.drag = false;
this.event = event;
}
coordinates(event: MouseEvent): void {
this.clientX = event.clientX;
this.clientY = event.clientY;
this.isMouseDown = event.buttons === 1;
}
ngOnInit() {
const _canvas = this.canvas.nativeElement;
const _photo = this.photo.nativeElement;
this.context = (<HTMLCanvasElement>_canvas).getContext('2d');
this.image = new Image();
this.image.src = '../../assets/images/1.jpg'
this.image.onload = () => {
this.context.drawImage(this.image, 0, 0, _canvas.width, _canvas.height);
console.log(this.image.src);
_photo.setAttribute('src', _canvas.toDataURL('image/png'));
this.profPic = _canvas.toDataURL('image/png');
};
console.log(this.clientX + ',' + this.clientY);
}
}
this is my css file...
#layout1{
border:1px solid #d3d3d3;
}
#subcanvas{
border:1px solid #d3d3d3;
}
.crope-image{
border:1px solid #d3d3d3;
}
}
here is my view now
Thank You...
Here is an example of how to move the circle with mouse down and stop it when mouseup
const canvas = document.querySelector('canvas')
const ctx = canvas.getContext('2d')
canvas.width = canvas.height = 150
let move = false
canvas.addEventListener('mouseup', e => {
move = false
})
canvas.addEventListener('mousedown', e => {
move = true
draw(e)
})
canvas.addEventListener('mousemove', e => {
draw(e)
})
function draw(e) {
if (move) {
ctx.clearRect(0, 0, canvas.width, canvas.height)
let x = e.clientX - canvas.offsetLeft
let y = e.clientY - canvas.offsetTop
ctx.beginPath();
ctx.arc(x, y, 10, 0, 2 * Math.PI, false);
ctx.fillText("x=" + x, 10,10);
ctx.fillText("y=" + y, 10,30);
ctx.fill();
}
}
<canvas style="border:1px solid #000000;"></canvas>
I have created an iFrame in my Google Chrome Extension popup and applied CSS so it has no border and height: 100%.
However, it appears with a limited height and shows a vertical scrollbar.
How do I set it to adjust to the height of the loaded HTML page?
This will work, source here.
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js?ver=1.3.2'></script>
<script type='text/javascript'>
$(function(){
var iFrames = $('iframe');
function iResize() {
for (var i = 0, j = iFrames.length; i < j; i++) {
iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';}
}
if ($.browser.safari || $.browser.opera) {
iFrames.load(function(){
setTimeout(iResize, 0);
});
for (var i = 0, j = iFrames.length; i < j; i++) {
var iSource = iFrames[i].src;
iFrames[i].src = '';
iFrames[i].src = iSource;
}
} else {
iFrames.load(function() {
this.style.height = this.contentWindow.document.body.offsetHeight + 'px';
});
}
});
</script>
<body style="margin:0px;padding:0px;overflow:hidden">
<iframe src="http://www.youraddress.com" frameborder="0" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;height:100%;width:100%;position:absolute;top:0px;left:0px;right:0px;bottom:0px" height="100%" width="100%"></iframe>
</body>
I have many elements with the same class on a web page. I would like to highlight all these elements when I hover one of them. How can I do that in CSS?
Right now, I have this CSS:
p.un:hover { background-color:yellow;}
And my HTML:
<div class="book">
<div class="page left">
<p class="un">Karen…</p>
</div>
<div class="page right">
<p class="un">Karen ne se retourne pas. Mme Tilford reste là, apparemment confuse et abattue.</p>
</div>
The best you can do using pure CSS is this:
.classname:hover ~ .classname {
background-color: yellow;
}
But this only highlights all siblings with a class classname after the hovered element.
You have to use JavaScript to highlight all elements;
var elms = document.getElementsByClassName("classname");
var n = elms.length;
function changeColor(color) {
for(var i = 0; i < n; i ++) {
elms[i].style.backgroundColor = color;
}
}
for(var i = 0; i < n; i ++) {
elms[i].onmouseover = function() {
changeColor("yellow");
};
elms[i].onmouseout = function() {
changeColor("white");
};
}
If you have multiple classes and want to generalize this, use something like this:
var classes = ["one", "two", "three"]; //list of your classes
var elms = {};
var n = {}, nclasses = classes.length;
function changeColor(classname, color) {
var curN = n[classname];
for(var i = 0; i < curN; i ++) {
elms[classname][i].style.backgroundColor = color;
}
}
for(var k = 0; k < nclasses; k ++) {
var curClass = classes[k];
elms[curClass] = document.getElementsByClassName(curClass);
n[curClass] = elms[curClass].length;
var curN = n[curClass];
for(var i = 0; i < curN; i ++) {
elms[curClass][i].onmouseover = function() {
changeColor(this.className, "yellow");
};
elms[curClass][i].onmouseout = function() {
changeColor(this.className, "white");
};
}
};
Thanks to the answer from Chris. I used his code to write a simple function that does the job. You can place the function in the <head></head> but you need to call it when the page has been loaded, i.e. place in the end of the body. colorout is the background-color
The function:
function hoverByClass(classname,colorover,colorout="transparent"){
var elms=document.getElementsByClassName(classname);
for(var i=0;i<elms.length;i++){
elms[i].onmouseover = function(){
for(var k=0;k<elms.length;k++){
elms[k].style.backgroundColor="orange";//colorover;
}
};
elms[i].onmouseout = function(){
for(var k=0;k<elms.length;k++){
elms[k].style.backgroundColor=colorout;
}
};
}
}
and call the function like this:
hoverByClass("un","yellow");
hoverByClass("un2","pink");
I know the question is old, but maybe that help someone else :)
Try it here:
function hoverByClass(classname,colorover,colorout="transparent"){
var elms=document.getElementsByClassName(classname);
for(var i=0;i<elms.length;i++){
elms[i].onmouseover = function(){
for(var k=0;k<elms.length;k++){
elms[k].style.backgroundColor=colorover;
}
};
elms[i].onmouseout = function(){
for(var k=0;k<elms.length;k++){
elms[k].style.backgroundColor=colorout;
}
};
}
}
hoverByClass("un","yellow");
hoverByClass("un2","pink");
<div class="book">
<div class="page left">
<p class="un">Karen…</p><p class="un2">Karen2…</p>
</div>
<div class="page right">
<p class="un">Karen ne se retourne pas. Mme Tilford reste là, apparemment confuse et abattue.</p><p class="un2">Karen2 ne se retourne pas. Mme Tilford2 reste là, apparemment confuse et abattue.</p>
</div>
</div>