Kinetics saving image error - jquery-mobile
Im having problems with kinetics. I have a stage with kinetics with a one image and text, but that I want is export the stage to a image like myImage.jpg no like [data:image/wIlksoks.e] that it is the callback that return dataUrl() from kinetics.
Im trying with this code:
stage.toDataURL({
width: 350,
height: 350,
mimeType: "image/jpeg",
callback: function(dataUrl) {
/*
* here you can do anything you like with the data url.
* In this tutorial we'll just open the url with the browser
* so that you can see the result as an image
*/
window.open(dataUrl);
}
});
}, false);
King Regards!
You can use stage.toDataURL to get your dataURL for the server:
stage.toDataURL({
callback:function(dataURL){
// dataURL is available for saving to your server
}
});
Note: Be sure that your image and your .html are hosted on the same domain.
Otherwise your stage.toImage will fail because of CORS security.
So be sure to check your console for CORS security errors !
Alternatively:
You can use stage.toImage to create a dataURL from your image+text.
Then you can create a temp canvas to get the dataURL.
stage.toImage({
callback:function(stageImg){
var tempCanvas=document.createElement("canvas");
var tempCtx=tempCanvas.getContext("2d");
tempCanvas.width=stageImg.width;
tempCanvas.height=stageImg.height;
tempCtx.drawImage(stageImg,0,0);
var dataURL=tempCanvas.toDataURL();
// dataURL is available for saving to your server
}
});
Here is code and a Fiddle: http://jsfiddle.net/m1erickson/RV694/
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Prototype</title>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.7.0.min.js"></script>
<style>
#container{
border:solid 1px #ccc;
margin-top: 10px;
width:300px;
height:300px;
}
</style>
<script>
$(function(){
var stage = new Kinetic.Stage({
container: 'container',
width: 300,
height: 300
});
var layer = new Kinetic.Layer();
stage.add(layer);
var img=new Image();
img.onload=function(){
start();
}
img.crossOrigin="anonymous";
img.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/KoolAidMan.png";
function start(){
var kImage = new Kinetic.Image({
x: 0,
y: 0,
width: 300,
height: 300,
image:img
});
layer.add(kImage);
var kText = new Kinetic.Text({
x:20,
y:20,
fontSize:24,
fill:"blue",
text:"Hello!"
});
layer.add(kText);
layer.draw();
}
$("#stageAsImage").click(function(){
stage.toImage({
callback:function(stageImg){
var tempCanvas=document.createElement("canvas");
var tempCtx=tempCanvas.getContext("2d");
tempCanvas.width=stageImg.width;
tempCanvas.height=stageImg.height;
tempCtx.drawImage(stageImg,0,0);
var dataURL=tempCanvas.toDataURL();
var imageElement=document.getElementById("newImage");
imageElement.src=dataURL;
}
});
});
}); // end $(function(){});
</script>
</head>
<body>
<button id="stageAsImage">Save stage as image</button>
<div id="container"></div>
<img id="newImage">
</body>
</html>
Related
RTSP (video stream) in Electron
I've installed fluent-ffmpeg and ffmpeg-static to convert a stream into an HTML reproducible video. How can I use these packages to display the stream in the client? Server side (main.js): const { app, BrowserWindow } = require('electron'); const ffmpeg = require('fluent-ffmpeg'); const ffmpegPath = require('ffmpeg-static').replace('app.asar', 'app.asar.unpacked'); ffmpeg.setFfmpegPath(ffmpegPath); function createWindow () { const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true } }); win.loadFile('index.html'); } function testStream () { let source = 'rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov'; } app.whenReady().then(createWindow).then(testStream); Client side (index.html): <!DOCTYPE html> <html> <head> <meta charset = 'UTF-8'> </head> <body> <h1>I want to reproduce the video here</h1> </body> </html>
If I understand correctly; you're probably looking to start with something like this: <video id="video-player" controls preload="none" style="border: 5px solid red; height: 1080px; width: 1920px; "> <source src="index.m3u8" type="application/x-mpegURL"> </video>
Chromecast + Youtube embed + HTML5 video tag = bug
I have been trying to play two videos (not necessarily simultaneously) on a Chromecast app. Once of them is embedded via the Youtube API and the other is a standard HTML5 video loaded via the tag. It turns out that the Youtube video simply won't be played back on Chromecast if I also have a standard tag in my HTML5 page. But if I remove that video tag, then the Youtube video will play back nicely. Any advice is highly appreciated!
Only one active video stream is supported on Chromecast.
I ran into this same problem. The key elements to solve it are, unload the cast player and don't let a video tag linger in the DOM with a SRC attribute set when you attempt to play Youtube. As for YouTube, dont let the IFRAME that contains their video player linger in the DOM either when attempting to use the HTML5 video tag. You'll need to tear down the previous player before attempting to trigger playback through the opposing mechanism. Here is my sample receiver I used to troubleshoot and solve the problem. This is a working example. <html> <head> <title></title> <style type="text/css"> body { background-color: #000; overflow: hidden; } </style> </head> <body> <script type="text/javascript" src="//code.jquery.com/jquery-2.1.4.min.js"></script> <script type="text/javascript" src="//www.gstatic.com/cast/sdk/libs/receiver/2.0.0/cast_receiver.js"></script> <script type="text/javascript" src="//www.gstatic.com/cast/sdk/libs/mediaplayer/1.0.0/media_player.js"></script> <video id="chromecast" style="width: 50%; height: 50%;"></video> <script> var ytCode = 'sSwLhYhYgI0' var hlsUrl = 'http://host.com/playlist.m3u8' </script> <!-- Chromecast --> <script type="text/javascript"> var castReceiverManager = cast.receiver.CastReceiverManager.getInstance(); var messageBus = castReceiverManager.getCastMessageBus('urn:x-cast:tv.domain'); cast.receiver.logger.setLevelValue(cast.receiver.LoggerLevel.DEBUG); cast.player.api.setLoggerLevel(cast.player.api.LoggerLevel.DEBUG); castReceiverManager.onReady = function() { console.info('[castReceiverManager.onReady]'); castReceiverManager.setApplicationState('Ready'); }; castReceiverManager.onSenderConnected = function(sender) { console.info('[castReceiverManager.onSenderConnected]', sender.userAgent); }; messageBus.onMessage = function(event) { var message = JSON.parse(event.data); console.info('[messageBus.onMessage]', message); }; // Normal castReceiverManager.start({ maxInactivity: 8, statusText: 'Ready to play', dialData: undefined }); window.playDirect = function(id) { // Tear down any YT player $('#youtube').remove(); if(!id) id = 'chromecast'; var mediaElement = document.getElementById(id); window.host = new cast.player.api.Host({'mediaElement':mediaElement, 'url':hlsUrl}); window.host.onError = function(errorCode) { console.log('ERROR ' + errorCode); }; var protocol = cast.player.api.CreateHlsStreamingProtocol(window.host); window.CCplayer = new cast.player.api.Player(window.host); window.CCplayer.load(protocol, 25); setTimeout(function() { mediaElement.play(); }, 1000); }; </script> <!-- Youtube --> <script> window.onYouTubeIframeAPIReady = function() { console.log('Initialized Youtube'); }; var playYT = function() { // Tear down any direct player if(window.CCplayer) { window.CCplayer.unload(); } $('video').attr('src', ''); // Inject div tag that will be converted to iframe with player $('body').append('<div id="youtube" style="width: 50%; height: 50%;"></div>'); window.YTPlayer = new YT.Player( 'youtube', { height: '100%', width: '100%', playerVars: { 'autoplay': 0, 'controls': 0, 'cc_load_policy': 0, 'fs': 0, 'iv_load_policy': 0, 'modestbranding': 0, 'rel': 0, 'showinfo': 0, 'enablejsapi': 1 }, events: { 'onReady': function() { var params = { videoId: ytCode, startSeconds: 170 }; window.YTPlayer.cueVideoById(params); window.YTPlayer.playVideo(); }, 'onError': function(err) { console.log('YT Error ' + err); } } }); }; var iframeScript = document.createElement('script'); iframeScript.src = "https://www.youtube.com/iframe_api"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(iframeScript, firstScriptTag); </script> </body> </html> Test: Load up the sample receiver above, and alternate the following inside the chrome debug console: playDirect() or playYT()
Canvas not defined
I am experimenting with Raymond Camden's watermark code and also example from Phonegap camera API. Camera works well. However, Uncaught ReferenceError: canvas is not defined is shown in LogCat. I have tried to place the variables in function as well as global. But the error still appears. My code is as below: <!DOCTYPE html> <head> <script type="text/javascript" charset="utf-8" src="cordova-2.7.0.js"></script> <link rel="stylesheet" type="text/css" href="jquery/css/jquery.mobile-1.3.1.min.css"/> <script type="text/javascript" charset="utf-8" src="jquery/jquery-1.9.1.min.js"></script> <script type="text/javascript" charset="utf-8" src="jquery/jquery.mobile-1.3.1.min.js"><script> <script type="text/javascript" charset="utf-8"> var watermark; var canvasDOM; var canvas; document.addEventListener("deviceready",onDeviceReady,false); function onDeviceReady() { } </script> <script type="text/javascript" charset="utf-8"> canvasDOM = $("myCanvas")[0]; canvas = canvasDOM.getContext("2d"); watermark = new Image(); watermark.src = "q1.jpg"; function cybershot() { navigator.camera.getPicture(camSuccess, camError, {quality: 75, targetWidth: 400, targetHeight: 400, destinationType: Camera.DestinationType.FILE_URI}); } function camError(e) { console.log("Camera Error"); console.log(JSON.stringify(e)); } function camSuccess(picuri) { console.log("Camera Success"); var img = new Image(); img.src=picuri; img.onload = function(e) { canvas.drawImage(img, 0, 0); canvas.drawImage(watermark, canvasDOM.width-watermark.width, canvasDOM.height - watermark.height); } } </script> <style> #myCanvas { width: 400px; height: 400px; } </style> </head> <body> <h1>Watermark Camera</h1> <button onclick="cybershot();">Capture Photo</button> <br> <p/> <canvas id="myCanvas"></canvas> </body> </html>
Solution There's an error in this code. Javascript used must be placed inside a onDeviceReady() function. It is an equivalent of classic jQuery document ready. In this case javascript is executed inside a HEAD before BODY content is loaded into the DOM. onDeviceReady() will delay its execution until everything is loaded into the DOM. Fixed code Your javascript should look like this: var canvas; var watermark; document.addEventListener("deviceready",onDeviceReady,false); function onDeviceReady() { canvasDOM = $("myCanvas")[0]; canvas = canvasDOM.getContext("2d"); watermark = new Image(); watermark.src = "q1.jpg"; } function cybershot() { navigator.camera.getPicture(camSuccess, camError, {quality: 75, targetWidth: 400, targetHeight: 400, destinationType: Camera.DestinationType.FILE_URI}); } function camError(e) { console.log("Camera Error"); console.log(JSON.stringify(e)); } function camSuccess(picuri) { console.log("Camera Success"); var img = new Image(); img.src=picuri; img.onload = function(e) { canvas.drawImage(img, 0, 0); canvas.drawImage(watermark, canvasDOM.width-watermark.width, canvasDOM.height - watermark.height); } }
Try moving this part (the entire content of the script block) canvasDOM = $("myCanvas")[0]; into $(document).ready(function(){ });
Using jQuery slider to change Google chart viewWindow
I have prepared a simple test case with screenshot, demonstrating my problem and am probably missing a tiny bit, just few lines of code. I have a diagram representing wins and losses in a web game over the weeks. I.e. the vertical axis represents the game score and the horizontal axis represents numbers of weeks. My ajax script returns the data for 52 weeks, but I'd like to add a slider and allow users change the viewed number of weeks to any number between 12 and 52. Can anybody please advise me, how to modify the change function? $("#money_slider").slider({ min: 12, max: 52, change: function(event, ui) { // XXX what to do here with // hAxis.viewWindow.min and .max? } }); Below is my complete test case, just save it to an .html file and you will be able to try it in a browser: <!DOCTYPE HTML> <html> <style type="text/css"> h3,p,div { text-align: center; } #slider { width: 700px; margin-left: auto; margin-right: auto; } </style> <style type="text/css" title="currentStyle"> #import "https://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css"; </style> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script> <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/jquery.dataTables.min.js"></script> <script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['corechart'],'language':'ru'}]}"></script> <script type="text/javascript"> google.setOnLoadCallback(drawChart); $(function() { $('#slider').slider({ disabled: true, range: 'min', min: 12, max: 52, change: function(event, ui) { // XXX what to do here with // hAxis.viewWindow.min and .max? $("#header").text("debug=" + ui.value); } }); }); function drawChart() { var jsonData = '{"cols":[{"label":"Week number","type":"string"},{"label":"Week leader","type":"number"},{"label":"Your win","type":"number"},{"label":"Your loss","type":"number"}],"rows":[{"c":[{"v":"2011-33"},{"v":10671},{"v":0},{"v":-3113}]},{"c":[{"v":"2011-34"},{"v":7975},{"v":0},{"v":-2113}]},{"c":[{"v":"2011-35"},{"v":11009},{"v":0},{"v":-2244}]},{"c":[{"v":"2011-36"},{"v":10679},{"v":0},{"v":-689}]},{"c":[{"v":"2011-37"},{"v":11197},{"v":305},{"v":0}]},{"c":[{"v":"2011-38"},{"v":6762},{"v":419},{"v":0}]},{"c":[{"v":"2011-39"},{"v":7823},{"v":0},{"v":-1563}]},{"c":[{"v":"2011-40"},{"v":10171},{"v":1152},{"v":0}]},{"c":[{"v":"2011-41"},{"v":9903},{"v":0},{"v":-1008}]},{"c":[{"v":"2011-42"},{"v":5940},{"v":0},{"v":-1332}]},{"c":[{"v":"2011-43"},{"v":7979},{"v":0},{"v":-593}]},{"c":[{"v":"2011-44"},{"v":7833},{"v":0},{"v":-653}]},{"c":[{"v":"2011-45"},{"v":9691},{"v":0},{"v":-562}]},{"c":[{"v":"2011-46"},{"v":8836},{"v":0},{"v":-1686}]},{"c":[{"v":"2011-47"},{"v":10358},{"v":0},{"v":-2120}]},{"c":[{"v":"2011-48"},{"v":9956},{"v":0},{"v":-1353}]},{"c":[{"v":"2011-49"},{"v":8787},{"v":160},{"v":0}]},{"c":[{"v":"2011-50"},{"v":9590},{"v":0},{"v":0}]},{"c":[{"v":"2011-51"},{"v":8931},{"v":887},{"v":0}]},{"c":[{"v":"2011-52"},{"v":8529},{"v":0},{"v":-1434}]},{"c":[{"v":"2012-01"},{"v":8680},{"v":0},{"v":-1416}]},{"c":[{"v":"2012-02"},{"v":9932},{"v":0},{"v":-169}]},{"c":[{"v":"2012-03"},{"v":8334},{"v":0},{"v":-3149}]},{"c":[{"v":"2012-04"},{"v":8077},{"v":217},{"v":0}]},{"c":[{"v":"2012-05"},{"v":7788},{"v":0},{"v":-3683}]},{"c":[{"v":"2012-06"},{"v":10070},{"v":113},{"v":0}]},{"c":[{"v":"2012-07"},{"v":8318},{"v":1704},{"v":0}]},{"c":[{"v":"2012-08"},{"v":8208},{"v":0},{"v":-104}]},{"c":[{"v":"2012-09"},{"v":11561},{"v":272},{"v":0}]},{"c":[{"v":"2012-10"},{"v":7797},{"v":0},{"v":0}]},{"c":[{"v":"2012-11"},{"v":9893},{"v":0},{"v":-90}]},{"c":[{"v":"2012-12"},{"v":9197},{"v":0},{"v":-191}]},{"c":[{"v":"2012-13"},{"v":7287},{"v":651},{"v":0}]},{"c":[{"v":"2012-14"},{"v":7072},{"v":646},{"v":0}]},{"c":[{"v":"2012-15"},{"v":7183},{"v":0},{"v":-907}]},{"c":[{"v":"2012-16"},{"v":6021},{"v":0},{"v":-993}]}]}'; var data = new google.visualization.DataTable(jsonData); var options = { width: 700, height: 500, legend: {position: 'top'}, areaOpacity: 1.0, vAxis: {format: '$#'}, hAxis: {title: 'Week number', titleTextStyle: {color: 'blue'}, slantedText: true}, colors: ['CCFFCC', '66CC66', 'FF9999'], animation: {duration: 1000, easing: 'out'} }; var chart = new google.visualization.SteppedAreaChart(document.getElementById('money')); google.visualization.events.addListener(chart, 'ready', function() { $('#slider').slider('enable'); }); chart.draw(data, options); } </script> </head> <body> <h3 id="header">Money</h3> <div id="money"></div> <div id="slider"></div> </body> </html> I've read the Google example (on the bottom), but don't grok it yet.
My own solution (has rendering problems with Opera 11.62 though - I've filed a bug #882 for that): <!DOCTYPE HTML> <html> <style type="text/css"> h3,p,div { text-align: center; } #slider { width: 700px; margin-left: auto; margin-right: auto; } </style> <style type="text/css" title="currentStyle"> #import "https://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css"; </style> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script> <script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['corechart'],'language':'ru'}]}"></script> <script type="text/javascript"> google.setOnLoadCallback(initChart); var jsonData = '{"cols":[{"label":"Week number","type":"string"},{"label":"Week leader","type":"number"},{"label":"Your win","type":"number"},{"label":"Your loss","type":"number"}],"rows":[{"c":[{"v":"2011-33"},{"v":10671},{"v":0},{"v":-3113}]},{"c":[{"v":"2011-34"},{"v":7975},{"v":0},{"v":-2113}]},{"c":[{"v":"2011-35"},{"v":11009},{"v":0},{"v":-2244}]},{"c":[{"v":"2011-36"},{"v":10679},{"v":0},{"v":-689}]},{"c":[{"v":"2011-37"},{"v":11197},{"v":305},{"v":0}]},{"c":[{"v":"2011-38"},{"v":6762},{"v":419},{"v":0}]},{"c":[{"v":"2011-39"},{"v":7823},{"v":0},{"v":-1563}]},{"c":[{"v":"2011-40"},{"v":10171},{"v":1152},{"v":0}]},{"c":[{"v":"2011-41"},{"v":9903},{"v":0},{"v":-1008}]},{"c":[{"v":"2011-42"},{"v":5940},{"v":0},{"v":-1332}]},{"c":[{"v":"2011-43"},{"v":7979},{"v":0},{"v":-593}]},{"c":[{"v":"2011-44"},{"v":7833},{"v":0},{"v":-653}]},{"c":[{"v":"2011-45"},{"v":9691},{"v":0},{"v":-562}]},{"c":[{"v":"2011-46"},{"v":8836},{"v":0},{"v":-1686}]},{"c":[{"v":"2011-47"},{"v":10358},{"v":0},{"v":-2120}]},{"c":[{"v":"2011-48"},{"v":9956},{"v":0},{"v":-1353}]},{"c":[{"v":"2011-49"},{"v":8787},{"v":160},{"v":0}]},{"c":[{"v":"2011-50"},{"v":9590},{"v":0},{"v":0}]},{"c":[{"v":"2011-51"},{"v":8931},{"v":887},{"v":0}]},{"c":[{"v":"2011-52"},{"v":8529},{"v":0},{"v":-1434}]},{"c":[{"v":"2012-01"},{"v":8680},{"v":0},{"v":-1416}]},{"c":[{"v":"2012-02"},{"v":9932},{"v":0},{"v":-169}]},{"c":[{"v":"2012-03"},{"v":8334},{"v":0},{"v":-3149}]},{"c":[{"v":"2012-04"},{"v":8077},{"v":217},{"v":0}]},{"c":[{"v":"2012-05"},{"v":7788},{"v":0},{"v":-3683}]},{"c":[{"v":"2012-06"},{"v":10070},{"v":113},{"v":0}]},{"c":[{"v":"2012-07"},{"v":8318},{"v":1704},{"v":0}]},{"c":[{"v":"2012-08"},{"v":8208},{"v":0},{"v":-104}]},{"c":[{"v":"2012-09"},{"v":11561},{"v":272},{"v":0}]},{"c":[{"v":"2012-10"},{"v":7797},{"v":0},{"v":0}]},{"c":[{"v":"2012-11"},{"v":9893},{"v":0},{"v":-90}]},{"c":[{"v":"2012-12"},{"v":9197},{"v":0},{"v":-191}]},{"c":[{"v":"2012-13"},{"v":7287},{"v":651},{"v":0}]},{"c":[{"v":"2012-14"},{"v":7072},{"v":646},{"v":0}]},{"c":[{"v":"2012-15"},{"v":7183},{"v":0},{"v":-907}]},{"c":[{"v":"2012-16"},{"v":6021},{"v":0},{"v":-993}]}]}'; var data = new google.visualization.DataTable(jsonData); var chart; var options = { width: 700, height: 500, legend: {position: 'top'}, areaOpacity: 1.0, vAxis: {format: '$#'}, hAxis: {title: 'Week number', titleTextStyle: {color: 'blue'}, slantedText: true, viewWindow: {min: 20, max: 35}}, colors: ['CCFFCC', '66CC66', 'FF9999'], animation: {duration: 1000, easing: 'out'} }; $(function() { $('#slider').slider({ disabled: true, range: 'min', value: 20, min: 0, max: 20, change: function(event, ui) { $('#header').text('debug=' + ui.value); options.hAxis.viewWindow.min = ui.value; drawChart(); } }); }); function initChart() { chart = new google.visualization.SteppedAreaChart(document.getElementById('money')); google.visualization.events.addListener(chart, 'ready', function() { $('#slider').slider('enable'); }); drawChart(); } function drawChart() { chart.draw(data, options); } </script> </head> <body> <h3 id="header">Money</h3> <div id="money"></div> <div id="slider"></div> </body> </html>
open camera in iPad phoneGap application
I am using following function for capture image. function openCamera() { navigator.camera.getPicture( function(uri) { var img = document.getElementById('camera_image'); img.style.visibility = "visible"; img.style.display = "block"; img.src = uri; }, function(e) { console.log("Error getting picture: " + e); }, { quality: 50, destinationType: navigator.camera.DestinationType.FILE_URI}); }; And I am using following js of phonegap <script type="text/javascript" charset="utf-8" src="phonegap-1.2.0.js"></script> I am using following image tag for captured image <img style="width: 128px; height: 160px; visibility:hidden; display:none;" id="camera_image" src="" /> But its not open camera.Am not know what is the issue.?
try adding the sourceType too.. sourceType : navigator.camera.PictureSourceType.CAMERA