Skip to content

Commit

Permalink
Add rotate-left button to bitmap editor toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
Jermolene committed Mar 12, 2018
1 parent 22a15be commit c056984
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 1 deletion.
4 changes: 4 additions & 0 deletions core/images/rotate-left.tid
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
title: $:/core/images/rotate-left
tags: $:/tags/Image

<svg class="tc-image-rotate-left tc-image-button" width="22pt" height="22pt" viewBox="0 0 128 128"><g fill-rule="evenodd"><rect width="32" height="80" rx="8"/><rect x="48" y="96" width="80" height="32" rx="8"/><path d="M61.32 36.65c19.743 2.45 35.023 19.287 35.023 39.693a4 4 0 0 1-8 0c0-15.663-11.254-28.698-26.117-31.46l3.916 3.916a4 4 0 1 1-5.657 5.657L49.172 43.142a4 4 0 0 1 0-5.657l11.313-11.313a4 4 0 1 1 5.657 5.656l-4.821 4.822z"/></g></svg>
2 changes: 2 additions & 0 deletions core/language/en-GB/Buttons.multids
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ PreviewType/Caption: preview type
PreviewType/Hint: Choose preview type
Quote/Caption: quote
Quote/Hint: Apply quoted text formatting to lines containing selection
RotateLeft/Caption: rotate left
RotateLeft/Hint: Rotate image left by 90 degrees
Size/Caption: image size
Size/Caption/Height: Height:
Size/Caption/Resize: Resize image
Expand Down
24 changes: 24 additions & 0 deletions core/modules/editor/operations/bitmap/rotate-left.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*\
title: $:/core/modules/editor/operations/bitmap/rotate-left.js
type: application/javascript
module-type: bitmapeditoroperation
Bitmap editor operation to rotate the image left by 90 degrees
\*/
(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

exports["rotate-left"] = function(event) {
// Rotate the canvas left by 90 degrees
this.rotateCanvasLeft();
// Update the input controls
this.refreshToolbar();
// Save the image into the tiddler
this.saveChanges();
};

})();
27 changes: 27 additions & 0 deletions core/modules/widgets/edit-bitmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,33 @@ EditBitmapWidget.prototype.changeCanvasSize = function(newWidth,newHeight) {
ctx.drawImage(this.currCanvas,0,0);
};

/*
** Rotate the canvas left by 90 degrees
*/
EditBitmapWidget.prototype.rotateCanvasLeft = function() {
// Get the current size of the image
var origWidth = this.canvasDomNode.width,
origHeight = this.canvasDomNode.height;
// Create and size a new canvas
var newCanvas = this.document.createElement("canvas"),
newWidth = origHeight,
newHeight = origWidth;
this.initCanvas(newCanvas,newWidth,newHeight);
// Copy the old image
var ctx = newCanvas.getContext("2d");
ctx.translate(newWidth / 2,newHeight / 2);
ctx.rotate(-Math.PI / 2);
ctx.drawImage(this.currCanvas,-origWidth / 2,-origHeight / 2);
// Set the new canvas as the current one
this.currCanvas = newCanvas;
// Set the size of the onscreen canvas
this.canvasDomNode.width = newWidth;
this.canvasDomNode.height = newHeight;
// Paint the onscreen canvas with the offscreen canvas
ctx = this.canvasDomNode.getContext("2d");
ctx.drawImage(this.currCanvas,0,0);
};

EditBitmapWidget.prototype.handleTouchStartEvent = function(event) {
this.brushDown = true;
this.strokeStart(event.touches[0].clientX,event.touches[0].clientY);
Expand Down
11 changes: 11 additions & 0 deletions core/ui/EditorToolbar/rotate-left.tid
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
title: $:/core/ui/EditorToolbar/rotate-left
tags: $:/tags/EditorToolbar
icon: $:/core/images/rotate-left
caption: {{$:/language/Buttons/RotateLeft/Caption}}
description: {{$:/language/Buttons/RotateLeft/Hint}}
condition: [<targetTiddler>is[image]]

<$action-sendmessage
$message="tm-edit-bitmap-operation"
$param="rotate-left"
/>
2 changes: 1 addition & 1 deletion core/wiki/tags/EditorToolbar.tid
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
title: $:/tags/EditorToolbar
list: $:/core/ui/EditorToolbar/paint $:/core/ui/EditorToolbar/opacity $:/core/ui/EditorToolbar/line-width $:/core/ui/EditorToolbar/clear $:/core/ui/EditorToolbar/bold $:/core/ui/EditorToolbar/italic $:/core/ui/EditorToolbar/strikethrough $:/core/ui/EditorToolbar/underline $:/core/ui/EditorToolbar/superscript $:/core/ui/EditorToolbar/subscript $:/core/ui/EditorToolbar/mono-line $:/core/ui/EditorToolbar/mono-block $:/core/ui/EditorToolbar/quote $:/core/ui/EditorToolbar/list-bullet $:/core/ui/EditorToolbar/list-number $:/core/ui/EditorToolbar/heading-1 $:/core/ui/EditorToolbar/heading-2 $:/core/ui/EditorToolbar/heading-3 $:/core/ui/EditorToolbar/heading-4 $:/core/ui/EditorToolbar/heading-5 $:/core/ui/EditorToolbar/heading-6 $:/core/ui/EditorToolbar/link $:/core/ui/EditorToolbar/excise $:/core/ui/EditorToolbar/picture $:/core/ui/EditorToolbar/stamp $:/core/ui/EditorToolbar/size $:/core/ui/EditorToolbar/editor-height $:/core/ui/EditorToolbar/more $:/core/ui/EditorToolbar/preview $:/core/ui/EditorToolbar/preview-type
list: $:/core/ui/EditorToolbar/paint $:/core/ui/EditorToolbar/opacity $:/core/ui/EditorToolbar/line-width $:/core/ui/EditorToolbar/rotate-left $:/core/ui/EditorToolbar/clear $:/core/ui/EditorToolbar/bold $:/core/ui/EditorToolbar/italic $:/core/ui/EditorToolbar/strikethrough $:/core/ui/EditorToolbar/underline $:/core/ui/EditorToolbar/superscript $:/core/ui/EditorToolbar/subscript $:/core/ui/EditorToolbar/mono-line $:/core/ui/EditorToolbar/mono-block $:/core/ui/EditorToolbar/quote $:/core/ui/EditorToolbar/list-bullet $:/core/ui/EditorToolbar/list-number $:/core/ui/EditorToolbar/heading-1 $:/core/ui/EditorToolbar/heading-2 $:/core/ui/EditorToolbar/heading-3 $:/core/ui/EditorToolbar/heading-4 $:/core/ui/EditorToolbar/heading-5 $:/core/ui/EditorToolbar/heading-6 $:/core/ui/EditorToolbar/link $:/core/ui/EditorToolbar/excise $:/core/ui/EditorToolbar/picture $:/core/ui/EditorToolbar/stamp $:/core/ui/EditorToolbar/size $:/core/ui/EditorToolbar/editor-height $:/core/ui/EditorToolbar/more $:/core/ui/EditorToolbar/preview $:/core/ui/EditorToolbar/preview-type

4 comments on commit c056984

@BurningTreeC
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Jermolene - I've tried the new rotate button and after rotating the canvas coordinates don't get updated immediately, one needs to save and open the tiddler again to be able to draw correctly

I guess that's a one-line fix somewhere, I'm just not getting where and how to do it, I'd make a PR otherwise

all the best, Simon

@Jermolene
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great spot, thanks @BurningTreeC I'll look into it...

@Jermolene
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @BurningTreeC; fixed in f1b38c4

@BurningTreeC
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @Jermolene

Please sign in to comment.