Auto save image or by button - css

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.

Related

Scroll animation on the canvas not working

Using the canvas, the image changes when scrolling.
I wrote the code for animation.
If css gives you the same value as positio:absolute top:4000px,
Even if I scroll, the animation doesn't work.
It works if you lower the top value. I have to operate at the top 4000px position.
I'm curious about the solution
var canvas = document.getElementById('macbook');
var ctx = canvas.getContext('2d');
var scrollYPos = 1000;
var img = new Image();
img.src = "img/AirPods Max/large0.jpg";
window.addEventListener('scroll', function(e) {
scrollYPos = Math.round (window.scrollY/5);
console.log(scrollYPos);
if (scrollYPos> 52) {
scrollYPos = 52;
}
player(scrollYPos);
});
function player(num) {
img.src = `img/AirPods Max/large${num}.jpg`;
}
img.addEventListener('load', function(e) {
ctx.clearRect(0,0,ctx.canvas.width, ctx.canvas.height);
ctx.drawImage(img, 0,0);
})
canvas {
position: absolute;
top: 4000px;
}
<div id="img-sequence">
<canvas width="1004" height="1214" id="macbook"></canvas>
</div>

how to move rectangle and take coordinates as globle to crop the selected image

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>

Images doesn't show in media print with Google Chrome

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.

Conditional fields in Drupal 7

I have a panel code pane that displays this code to show/hide the content if a field, it works great but I want the code to just not display anything if there is no content in %node:field_desc6
<script language="javascript">
function toggle() {
var ele = document.getElementById("toggleText");
var text = document.getElementById("displayText");
if(ele.style.display == "block") {
ele.style.display = "none";
text.innerHTML = "Show Non-CGD Publications";
}
else {
ele.style.display = "block";
text.innerHTML = "Hide Non-CGD Publications";
}
}
</script>
<a id="displayText" href="javascript:toggle();">Show Non-CGD Publications</a>
<div id="toggleText" style="display: none">%node:field_desc6</div>
Try This :
<script language="javascript">
function toggle() {
var ele = document.getElementById("toggleText");
var text = document.getElementById("displayText");
if(ele.style.display == "block") {
ele.style.display = "none";
text.innerHTML = "Show Non-CGD Publications";
}
else {
ele.style.display = "block";
text.innerHTML = ""; // here you don't put any text...
}
}
</script>
<a id="displayText" href="javascript:toggle();">Show Non-CGD Publications</a>
<div id="toggleText" style="display: none">%node:field_desc6</div>

Chrome Extension - iFrame auto-height

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>

Resources