Advertisement
Guest User

Untitled

a guest
Jul 11th, 2012
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Copyright 2012 OCAD University
  3.  
  4. Licensed under the Educational Community License (ECL), Version 2.0 or the New
  5. BSD license. You may not use this file except in compliance with one these
  6. Licenses.
  7.  
  8. You may obtain a copy of the ECL 2.0 License and BSD License at
  9. https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
  10. */
  11.  
  12. // Declare dependencies
  13. /*jQuery, fluid*/
  14.  
  15. // JSLint options
  16. /*jslint white: true, undef: true, newcap: true, nomen: true, regexp: true, bitwise: true, browser: true, forin: true, maxerr: 100, indent: 4 */
  17.  
  18. var editor = editor || {};
  19.  
  20. (function ($, fluid) {
  21.  
  22.   /**
  23.     * ImageAdjuster Infusion Component
  24.     */
  25.  
  26.   "use strict";
  27.  
  28.     fluid.defaults("editor.imageAdjuster", {
  29.         gradeNames: ["fluid.viewComponent", "autoInit"],
  30.         preInitFunction: "editor.imageAdjuster.preInit",
  31.         postInitFunction: "editor.imageAdjuster.postInit",
  32.         selectors : {
  33.                 container : '#flc-image-adjuster-container',
  34.                 canvas : '#flc-image-adjuster-canvas',
  35.                 brightnessTab : '#flc-image-adjuster-brightness',
  36.                 rotateTab : '#flc-image-adjuster-rotate',
  37.                 thresholdTab : '#flc-image-adjuster-threshold',
  38.                 brightnessMenu : '#flc-image-adjuster-brightness-controls',
  39.                 rotateMenu : '#flc-image-adjuster-rotate-controls',
  40.                 thresholdMenu : '#flc-image-adjuster-threshold-controls'
  41.         },
  42.         options: {
  43.  
  44.         }
  45.     });
  46.        
  47.     editor.imageAdjuster.adjustments = {
  48.         brightness: 0,
  49.         contrast: 0,
  50.         threshold: 0,
  51.         rotation: 0
  52.     };
  53.        
  54.     editor.imageAdjuster.preInit = function (that) {
  55.        
  56.         var image;
  57.         //load canvas
  58.         that.canvas = that.locate('canvas');
  59.         that.canvas = $('#flc-image-adjuster-canvas')[0];
  60.         that.ctx = that.canvas.getContext('2d');
  61.         that.image = new Image();
  62.         that.image.src = '../webapp/demo.jpg';
  63.         that.ctx.drawImage(that.image, 0, 0);
  64.        
  65.         editor.imageAdjuster.bindEvents(that);
  66.     };
  67.  
  68.     editor.imageAdjuster.postInit = function (that) {
  69.         that.locate('rotateMenu');
  70.     };
  71.  
  72.     editor.imageAdjuster.setRotate = function (degree) {
  73.  
  74.         var maxVal, minVal;
  75.  
  76.         //bounds check
  77.         maxVal = 360;
  78.         minVal = -360;
  79.        
  80.         if(degree <= minVal) {
  81.             degree = minVal;
  82.         }      
  83.         else if(degree >= maxVal) {
  84.             degree = maxVal;
  85.         }
  86.  
  87.         editor.imageAdjuster.adjustments.rotation += degree;
  88.        
  89.         return degree;
  90.  
  91.     };
  92.    
  93.     editor.imageAdjuster.rotate = function(that) {
  94.        
  95.         var centerX, centerY, degrees, radians;
  96.        
  97.         centerX = that.canvas.width/2;
  98.         centerY = that.canvas.height/2;
  99.         degrees = editor.imageAdjuster.adjustments.rotation;
  100.         radians = degrees*Math.PI/180;
  101.        
  102.         //rotate canvas
  103.         that.ctx.save();
  104.         that.ctx.translate(centerX, centerY);
  105.         that.ctx.rotate(radians);
  106.         that.ctx.translate(-(centerX), -(centerY));
  107.         that.ctx.drawImage(that.image, 0, 0);
  108.         that.ctx.restore();
  109.        
  110.         return true;
  111.  
  112.     };
  113.  
  114.     editor.imageAdjuster.bindEvents = function (that) {
  115.        
  116.         var rotateTab, rotateMenu, rotateInput, rotateApply, activeMenu;
  117.        
  118.         //rotateTab = that.locate('rotateTab');
  119.         rotateTab = $('#flc-image-adjuster-rotate');
  120.         rotateMenu = $('#flc-image-adjuster-rotate-controls');
  121.         rotateInput = $('[name=flc-image-adjuster-rotate]');
  122.         rotateApply = $('#flc-image-adjuster-rotate-apply');
  123.  
  124.         activeMenu = $('.flc-image-adjuster-controls');
  125.  
  126.         rotateTab.click(function () {
  127.             activeMenu.hide();
  128.             rotateMenu.show();
  129.         });
  130.  
  131.         rotateApply.click(function () {
  132.         var rotateValue;
  133.             rotateValue = rotateInput.val();
  134.             editor.imageAdjuster.setRotate(rotateValue);
  135.             editor.imageAdjuster.rotate(that);
  136.         });    
  137.  
  138.     };
  139.  
  140. }(jQuery, fluid));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement