There is a combination of errors.
- Jahdriens filereader takes care of the implementation of the flash. Just turn on the swfObject library.
- Browser sniffing = bad idea. Modernizr = good idea.
- Make sure you have flash installed for IE :(
My latest code looks like this and it works great. HTML:
<canvas id="mainCanvas" width="600" height="600"></canvas><br /> <a id="imageLoaderButton" class="button upload">load image</a> <input type="file" id="imageLoader" class="hidden" name="imageLoader" /> <input id="text" type="text" placeholder="some text..."> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.8.1.min.js"><\/script>')</script> <script src="js/main.js"></script>
+ link in the custom assembly modernizr in the head. (when creating a custom assembly, click "non-kernel detects" β "api file")
And my JS:
$(function () { Modernizr.load({ test: Modernizr.filereader, nope: ['js/vendor/swfobject.js', 'js/vendor/jquery-ui-1.8.23.custom.min.js', 'js/vendor/jquery.FileReader.min.js'], complete: function () { if (!Modernizr.filereader) { $('#imageLoaderButton').fileReader({ id: 'fileReaderSWFObject', filereader: 'filereader.swf', expressInstall: 'expressInstall.swf', debugMode: true, callback: function () { $('#imageLoaderButton').show().on('change', read); } }); } else { $('#imageLoaderButton').show().on('click', function () { $('#imageLoader').trigger('click').on('change', read); }); } } }); // Variables var canvas = document.getElementById('mainCanvas'); var context = canvas.getContext('2d'); var canvasCenter = canvas.width / 2; var img = ''; var padding = 50; // Functions var flushCanvas = function () { context.fillStyle = '#000'; context.fillRect(0, 0, canvas.width, canvas.width + padding); if (img !== '') { context.drawImage(img, padding, padding, canvas.width - (padding * 2), newImageHeight - (padding * 2)); } setText(); }; var setText = function () { context.textAlign = 'center'; context.fillStyle = '#fff'; context.font = '22px sans-serif'; context.textBaseline = 'bottom'; context.fillText($('#text').val(), canvasCenter, canvas.height - 40); }; var read = function (e) { if (typeof FileReader !== 'undefined') { var reader = new FileReader(); reader.onload = function (event) { img = new Image(); img.onload = function () { newImageHeight = (img.height / img.width) * (canvas.width); canvas.height = newImageHeight + padding; flushCanvas(); } img.src = event.target.result; } reader.readAsDataURL(e.target.files[0]); } }; $('#text').keyup(function (e) { flushCanvas(); }); });
source share